Skip to content

Instantly share code, notes, and snippets.

@whoeverest
Last active November 20, 2015 16:14
Show Gist options
  • Save whoeverest/83ab2ba7cc9d85ce5f6e to your computer and use it in GitHub Desktop.
Save whoeverest/83ab2ba7cc9d85ce5f6e to your computer and use it in GitHub Desktop.
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
Copy link

spion commented Nov 20, 2015

class CodedError extends Error { 
    constructor(public code:number, public message:string) { 
        super(message)
        if (typeof (<any>Error).captureStackTrace === 'function') {
            (<any>Error).captureStackTrace(this, CodedError)
        }
    } 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment