Skip to content

Commit

Permalink
fix: include original error and add extra info
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoniangreen committed Aug 7, 2024
1 parent d33a36d commit 4e3463d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/vocabularies/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function usePattern({gen, it: {opts, errSchemaPath}}: KeywordCxt, pattern
try {
rx = new RegExp(pattern, u)
} catch (e) {
throw new Error(`Invalid regular expression: ${pattern} at ${errSchemaPath}`)
throw new Error(`${(e as Error).message} | pattern ${pattern} at ${errSchemaPath}`)
}
rx = regExp(pattern, u)

Expand Down
2 changes: 1 addition & 1 deletion lib/vocabularies/validation/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const def: CodeKeywordDefinition = {
try {
return new RegExp(${schemaCode}, ${u})
} catch (e) {
throw new Error('Invalid regular expression: ' + ${schemaCode} + ' at ' + ${it.errSchemaPath})
throw new Error(e.message + ' | pattern ' + ${schemaCode} + ' at ' + ${it.errSchemaPath})
}
})()`
: usePattern(cxt, schema)
Expand Down
9 changes: 6 additions & 3 deletions spec/issues/2477_informative_pattern_errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ describe("Invalid regexp patterns should throw more informative errors (issue #2
assert.throws(
() => ajv.compile(rootSchema),
(thrown: unknown) => {
assert.equal((thrown as Error).message, "Invalid regular expression: ^[0-9]{2-4} at #")
assert.equal(
(thrown as Error).message,
"Invalid regular expression: /^[0-9]{2-4}/: Incomplete quantifier | pattern ^[0-9]{2-4} at #"
)
return true
}
)
Expand All @@ -30,7 +33,7 @@ describe("Invalid regexp patterns should throw more informative errors (issue #2
(thrown: unknown) => {
assert.equal(
(thrown as Error).message,
"Invalid regular expression: ^[0-9]{2-4} at #/properties/foo"
"Invalid regular expression: /^[0-9]{2-4}/: Incomplete quantifier | pattern ^[0-9]{2-4} at #/properties/foo"
)
return true
}
Expand All @@ -52,7 +55,7 @@ describe("Invalid regexp patterns should throw more informative errors (issue #2
(thrown: unknown) => {
assert.equal(
(thrown as Error).message,
"Invalid regular expression: ^[0-9]{2-4} at #/properties/string"
"Invalid regular expression: /^[0-9]{2-4}/: Incomplete quantifier | pattern ^[0-9]{2-4} at #/properties/string"
)
return true
}
Expand Down

0 comments on commit 4e3463d

Please sign in to comment.