Skip to content

Instantly share code, notes, and snippets.

@spion
Forked from whoeverest/coded-error.js
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/51bc9b9a3247bbfa0fda to your computer and use it in GitHub Desktop.
Save spion/51bc9b9a3247bbfa0fda to your computer and use it in GitHub Desktop.
export class CodedError extends Error {
constructor(code, msg) {
super(msg)
this.code = code;
this.msg = msg;
}
static is(code) {
return (e) => e.code == code;
}
static only(code, handler) {
return err => {
if (err.code === code) return handler(err)
throw err;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment