-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement isCError & isWError methods
These methods allows consumers to check the type of error recieved, without resorting to `if(err instanceof CError)` which can prove quite error-prone across different versions of the library.
- Loading branch information
1 parent
ae87fb5
commit 62b52ea
Showing
5 changed files
with
51 additions
and
2 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
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 |
---|---|---|
@@ -1,15 +1,22 @@ | ||
import CError, { Options } from './cerror'; | ||
|
||
export const WERROR_SYMBOL = Symbol.for('contextual-error/werror'); | ||
|
||
export class WError extends CError { | ||
public readonly name: string = 'WError'; | ||
|
||
constructor(message?: string, cause?: Error, options?: Options) { | ||
super(message, cause, Object.assign(options || {}, { skipCauseMessage: true })); | ||
Object.defineProperty(this, WERROR_SYMBOL, { value: true }); | ||
|
||
if (options?.name) { | ||
this.name = options.name; | ||
} | ||
} | ||
|
||
public static isWError(obj: unknown): boolean { | ||
return (obj as { [WERROR_SYMBOL]?: boolean })?.[WERROR_SYMBOL] != null; | ||
} | ||
} | ||
|
||
export default WError; |