-
Notifications
You must be signed in to change notification settings - Fork 238
Refactor app validation errors: result-based parsing, structured error data #7126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3bf51b5
Refactor app validation errors: result-based parsing, structured erro…
ryancbahan 86abe0f
Refactor app validation errors: result-based parsing, structured erro…
ryancbahan 3249375
Refactor app validation errors: result-based parsing, structured erro…
ryancbahan fb5f127
Refactor app validation errors: result-based parsing, structured erro…
ryancbahan 21329b0
Refactor app validation errors: result-based parsing, structured erro…
ryancbahan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,237 +1,91 @@ | ||
| import {parseHumanReadableError} from './error-parsing.js' | ||
| import {parseStructuredErrors} from './error-parsing.js' | ||
| import {describe, expect, test} from 'vitest' | ||
|
|
||
| describe('parseHumanReadableError', () => { | ||
| test('formats union errors with smart variant detection', () => { | ||
| const unionErrorObject = [ | ||
| { | ||
| code: 'invalid_union', | ||
| unionErrors: [ | ||
| { | ||
| issues: [ | ||
| { | ||
| code: 'invalid_type', | ||
| expected: 'array', | ||
| received: 'number', | ||
| path: ['web', 'roles'], | ||
| message: 'Expected array, received number', | ||
| }, | ||
| { | ||
| code: 'invalid_type', | ||
| expected: 'string', | ||
| received: 'undefined', | ||
| path: ['web', 'commands', 'build'], | ||
| message: 'Required', | ||
| }, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| { | ||
| issues: [ | ||
| { | ||
| code: 'invalid_literal', | ||
| expected: "'frontend'", | ||
| received: 'number', | ||
| path: ['web', 'type'], | ||
| message: "Invalid literal value, expected 'frontend'", | ||
| }, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| ], | ||
| path: ['web'], | ||
| message: 'Invalid input', | ||
| }, | ||
| describe('parseStructuredErrors', () => { | ||
| test('converts regular issues to structured format', () => { | ||
| const issues = [ | ||
| {path: ['name'], message: 'Required', code: 'invalid_type'}, | ||
| {path: ['version'], message: 'Must be a string', code: 'invalid_type'}, | ||
| ] | ||
|
|
||
| const result = parseHumanReadableError(unionErrorObject) | ||
|
|
||
| // Verify the enhanced format shows only the best matching variant's errors | ||
| // (Option 1 has both missing field + type error, so it's likely the intended one) | ||
| expect(result).toContain('[web.roles]: Expected array, received number') | ||
| expect(result).toContain('[web.commands.build]: Required') | ||
|
|
||
| // Should NOT show confusing union variant breakdown | ||
| expect(result).not.toContain('Union validation failed') | ||
| expect(result).not.toContain('Option 1:') | ||
| expect(result).not.toContain('Option 2:') | ||
| const result = parseStructuredErrors(issues) | ||
|
|
||
| // Should NOT show errors from the less likely option 2 | ||
| expect(result).not.toContain("[web.type]: Invalid literal value, expected 'frontend'") | ||
| expect(result).toEqual([ | ||
| {path: ['name'], message: 'Required', code: 'invalid_type'}, | ||
| {path: ['version'], message: 'Must be a string', code: 'invalid_type'}, | ||
| ]) | ||
| }) | ||
|
|
||
| test('handles regular non-union errors', () => { | ||
| const regularErrorObject = [ | ||
| { | ||
| path: ['name'], | ||
| message: 'Required field is missing', | ||
| }, | ||
| { | ||
| path: ['version'], | ||
| message: 'Must be a valid semver string', | ||
| }, | ||
| ] | ||
|
|
||
| const result = parseHumanReadableError(regularErrorObject) | ||
|
|
||
| // Verify regular errors still work as expected | ||
| expect(result).toBe('• [name]: Required field is missing\n• [version]: Must be a valid semver string\n') | ||
| expect(result).not.toContain('Union validation failed') | ||
| }) | ||
|
|
||
| test('handles edge cases for union error detection', () => { | ||
| // Test case 1: Union error with no unionErrors array | ||
| const noUnionErrors = [ | ||
| { | ||
| code: 'invalid_union', | ||
| path: ['root'], | ||
| message: 'Invalid input', | ||
| }, | ||
| ] | ||
|
|
||
| const result1 = parseHumanReadableError(noUnionErrors) | ||
| expect(result1).toBe('• [root]: Invalid input\n') | ||
|
|
||
| // Test case 2: Union error with empty unionErrors array - should fall back to showing full union error | ||
| const emptyUnionErrors = [ | ||
| { | ||
| code: 'invalid_union', | ||
| unionErrors: [], | ||
| path: ['root'], | ||
| message: 'Invalid input', | ||
| }, | ||
| ] | ||
|
|
||
| const result2 = parseHumanReadableError(emptyUnionErrors) | ||
| expect(result2).toContain("Configuration doesn't match any expected format:") | ||
|
|
||
| // Test case 3: Union errors with variants that have no issues - results in empty string | ||
| const noIssuesVariants = [ | ||
| { | ||
| code: 'invalid_union', | ||
| unionErrors: [ | ||
| {issues: [], name: 'ZodError'}, | ||
| {issues: [], name: 'ZodError'}, | ||
| ], | ||
| path: ['root'], | ||
| message: 'Invalid input', | ||
| }, | ||
| ] | ||
|
|
||
| const result3 = parseHumanReadableError(noIssuesVariants) | ||
| // When all variants have no issues, the best variant selection returns null issues | ||
| // resulting in no output, which falls back to the union error display | ||
| expect(result3).toContain("Configuration doesn't match any expected format:") | ||
| }) | ||
|
|
||
| test('findBestMatchingVariant scoring logic works correctly', () => { | ||
| // Test various scoring scenarios by creating mock union errors | ||
| const scenarioWithMissingFields = [ | ||
| test('selects best union variant based on scoring', () => { | ||
| const issues = [ | ||
| { | ||
| code: 'invalid_union', | ||
| unionErrors: [ | ||
| { | ||
| // Variant with missing fields - should score highest | ||
| issues: [ | ||
| {path: ['supports_moto'], message: 'Required'}, | ||
| {path: ['merchant_label'], message: 'Required'}, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| { | ||
| // Variant with only type errors - should score lower | ||
| issues: [ | ||
| {path: ['type'], message: 'Expected string, received number'}, | ||
| {path: ['id'], message: 'Expected number, received string'}, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| { | ||
| // Variant with other errors - should score lowest | ||
| issues: [{path: ['url'], message: 'Invalid URL format'}], | ||
| name: 'ZodError', | ||
| }, | ||
| ], | ||
| path: [], | ||
| message: 'Invalid input', | ||
| }, | ||
| ] | ||
|
|
||
| const result = parseHumanReadableError(scenarioWithMissingFields) | ||
| const result = parseStructuredErrors(issues) | ||
|
|
||
| // Should show only the variant with missing fields (highest score) | ||
| expect(result).toContain('[supports_moto]: Required') | ||
| expect(result).toContain('[merchant_label]: Required') | ||
|
|
||
| // Should NOT show errors from other variants | ||
| expect(result).not.toContain('Expected string, received number') | ||
| expect(result).not.toContain('Invalid URL format') | ||
| expect(result).not.toContain('Union validation failed') | ||
| expect(result).toEqual([ | ||
| {path: ['supports_moto'], message: 'Required', code: 'invalid_union'}, | ||
| {path: ['merchant_label'], message: 'Required', code: 'invalid_union'}, | ||
| ]) | ||
| }) | ||
|
|
||
| test('handles undefined messages gracefully', () => { | ||
| const undefinedMessageError = [ | ||
| { | ||
| path: ['field'], | ||
| message: undefined, | ||
| }, | ||
| test('falls back when all union variants have empty issues', () => { | ||
| const issues = [ | ||
| { | ||
| path: [], | ||
| // message is completely missing | ||
| code: 'invalid_union', | ||
| unionErrors: [ | ||
| {issues: [], name: 'ZodError'}, | ||
| {issues: [], name: 'ZodError'}, | ||
| ], | ||
| path: ['root'], | ||
| message: 'Invalid input', | ||
| }, | ||
| ] | ||
|
|
||
| const result = parseHumanReadableError(undefinedMessageError) | ||
| const result = parseStructuredErrors(issues) | ||
|
|
||
| expect(result).toBe('• [field]: Unknown error\n• [root]: Unknown error\n') | ||
| expect(result).toEqual([{path: ['root'], message: 'Invalid input', code: 'invalid_union'}]) | ||
| }) | ||
|
|
||
| test('handles mixed scoring scenarios', () => { | ||
| // Test scenario where we need to pick between variants with different error combinations | ||
| const mixedScenario = [ | ||
| test('falls back when union has no unionErrors array', () => { | ||
| const issues = [ | ||
| { | ||
| code: 'invalid_union', | ||
| unionErrors: [ | ||
| { | ||
| // Mix of missing fields and type errors - this should win due to missing fields | ||
| issues: [ | ||
| {path: ['required_field'], message: 'Required'}, | ||
| {path: ['wrong_type'], message: 'Expected string, received number'}, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| { | ||
| // Only type errors - should lose | ||
| issues: [ | ||
| {path: ['field1'], message: 'Expected boolean, received string'}, | ||
| {path: ['field2'], message: 'Expected array, received object'}, | ||
| {path: ['field3'], message: 'Expected number, received string'}, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| { | ||
| // Only other validation errors - should score lowest | ||
| issues: [ | ||
| {path: ['url'], message: 'Must be valid URL'}, | ||
| {path: ['email'], message: 'Must be valid email'}, | ||
| ], | ||
| name: 'ZodError', | ||
| }, | ||
| ], | ||
| path: [], | ||
| path: ['field'], | ||
| message: 'Invalid input', | ||
| }, | ||
| ] | ||
|
|
||
| const result = parseHumanReadableError(mixedScenario) | ||
| const result = parseStructuredErrors(issues) | ||
|
|
||
| expect(result).toEqual([{path: ['field'], message: 'Invalid input', code: 'invalid_union'}]) | ||
| }) | ||
|
|
||
| test('handles missing message with fallback', () => { | ||
| const issues = [{path: ['x'], code: 'custom'}] | ||
|
|
||
| // Should pick the variant with missing field (even though it has fewer total errors) | ||
| expect(result).toContain('[required_field]: Required') | ||
| expect(result).toContain('[wrong_type]: Expected string, received number') | ||
| const result = parseStructuredErrors(issues) | ||
|
|
||
| // Should not show errors from other variants | ||
| expect(result).not.toContain('Expected boolean, received string') | ||
| expect(result).not.toContain('Must be valid URL') | ||
| expect(result).not.toContain('Union validation failed') | ||
| expect(result).toEqual([{path: ['x'], message: 'Unknown error', code: 'custom'}]) | ||
| }) | ||
| }) |
ryancbahan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this catch may be a bit too broad for the JSON contract.
linkedAppContext()does more than local config validation — it can also abort during link / remote app lookup / org fetch / spec fetch. For example,appFromIdentifiers()inservices/context.tsthrowsAbortErrorfor cases like “No app with client ID … found”.So with this change, some operational/setup failures now look like:
{"valid": false, "errors": [{"message": "..."}]}which reads as “your config is invalid” even when the problem is actually linking/auth/remote resolution.
I think the
--jsonfallback should probably only map actual local configuration failures into the validation result shape, and let unrelatedAbortErrors bubble normally. Otherwise machine consumers may start treating non-validation failures as fixable TOML issues.Apologies if we've litigated this already, but just want to confirm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the top pr to address this. It's a little hacky while we only have AbortError today, but I think we can continue to refine.