-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: throw error on any predicate only if there are any chore: make default message empty Doesn't matter much as it's overwritten immediately after chore: drop redundant nullish coalescing fix: declare the validationErrors prop as readonly Gives a TS compilation error if you try to reassign it chore: generate error message only if there are errors chore: fix bug that caused validators to error if validator func throws Noticeable with a function like ```js ow( null, ow.any( ow.string.minLength(12), ow.string.includes('owo') ) ); ``` chore: apply code style suggestion Co-authored-by: Sindre Sorhus <[email protected]> chore: fix stack generation and comment chore: move from t.assert to t.is src: add more tests to increase coverage chore: correct test messages chore: fix rebase
- Loading branch information
1 parent
b4a2ada
commit 1a91170
Showing
14 changed files
with
418 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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,17 +1,22 @@ | ||
const wrapStackTrace = (error: ArgumentError, stack: string) => `${error.name}: ${error.message}\n${stack}`; | ||
|
||
/** | ||
@hidden | ||
*/ | ||
export class ArgumentError extends Error { | ||
constructor(message: string, context: Function) { | ||
readonly validationErrors: ReadonlyMap<string, string[]>; | ||
|
||
constructor(message: string, context: Function, stack: string, errors = new Map<string, string[]>()) { | ||
super(message); | ||
|
||
this.name = 'ArgumentError'; | ||
|
||
if (Error.captureStackTrace) { | ||
// TODO: Node.js does not preserve the error name in output when using the below, why? | ||
Error.captureStackTrace(this, context); | ||
} else { | ||
this.stack = (new Error()).stack; | ||
this.stack = wrapStackTrace(this, stack); | ||
} | ||
|
||
this.name = 'ArgumentError'; | ||
this.validationErrors = errors; | ||
} | ||
} |
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
Generates a complete message from all errors generated by predicates. | ||
@param errors - The errors generated by the predicates. | ||
@param isAny - If this function is called from the any argument. | ||
@hidden | ||
*/ | ||
export const generateArgumentErrorMessage = (errors: Map<string, string[]>, isAny = false) => { | ||
const message = []; | ||
|
||
const errorArray = [...errors.values()]; | ||
|
||
const anyErrorWithoutOneItemOnly = errorArray.some(array => array.length !== 1); | ||
|
||
// If only one error "key" is present, enumerate all of those errors only. | ||
if (errors.size === 1) { | ||
const returnedErrors = errorArray[0]!; | ||
|
||
if (!isAny && returnedErrors.length === 1) { | ||
return returnedErrors[0]!; | ||
} | ||
|
||
for (const entry of returnedErrors) { | ||
message.push(`${isAny ? ' - ' : ''}${entry}`); | ||
} | ||
|
||
return message.join('\n'); | ||
} | ||
|
||
// If every predicate returns just one error, enumerate them as is. | ||
if (!anyErrorWithoutOneItemOnly) { | ||
return errorArray.map(([item]) => ` - ${item}`).join('\n'); | ||
} | ||
|
||
// Else, iterate through all the errors and enumerate them. | ||
for (const [key, value] of errors) { | ||
message.push(`Errors from the "${key}" predicate:`); | ||
for (const entry of value) { | ||
message.push(` - ${entry}`); | ||
} | ||
} | ||
|
||
return message.join('\n'); | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
Generates a useful stacktrace that points to the user's code where the error happened on platforms without the `Error.captureStackTrace()` method. | ||
@hidden | ||
*/ | ||
export const generateStackTrace = () => { | ||
const stack = new RangeError('INTERNAL_OW_ERROR').stack!; | ||
|
||
return stack; | ||
}; |
This file contains 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.