Last active
November 20, 2015 16:14
-
-
Save whoeverest/83ab2ba7cc9d85ce5f6e to your computer and use it in GitHub Desktop.
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
export class CodedError extends Error { | |
constructor(public code:number, public message:string) { | |
super(message) | |
if (typeof (<any>Error).captureStackTrace === 'function') { | |
(<any>Error).captureStackTrace(this, CodedError) | |
} | |
} | |
static is(code: number) { | |
return (e: CodedError) => e.code == code; | |
} | |
static only(code: number, handler: (e: any) => any) { | |
return (err: CodedError) => { | |
if (err.code === code) return handler(err) | |
throw err; | |
} | |
} | |
} |
spion
commented
Nov 20, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment