Skip to content

Instantly share code, notes, and snippets.

@jaredpalmer
Created November 30, 2016 14:43
Show Gist options
  • Save jaredpalmer/1a185f5c724510d648b81fcdffdcdf85 to your computer and use it in GitHub Desktop.
Save jaredpalmer/1a185f5c724510d648b81fcdffdcdf85 to your computer and use it in GitHub Desktop.
errors
'use strict'
const uuid = require('node-uuid')
class APIError extends Error {
constructor (opts) {
super(opts)
Error.captureStackTrace(this, APIError)
this.name = this.constructor.name || 'APIError'
this.code = opts.code || 500
this.level = opts.level || 'normal'
this.id = uuid.v4()
}
}
class NotFoundError extends APIError {
constructor (message = 'Not found', code = 404) {
super(message, code)
}
}
class ForbiddenError extends APIError {
constructor (message = 'Forbidden', code = 403) {
super(message, code)
}
}
class UnauthorizedError extends APIError {
constructor (message = 'Unauthorized', code = 401) {
super(message, code)
}
}
class BadRequestError extends APIError {
constructor (message = 'Bad request data', code = 400, fields = {}) {
super(message, code)
this.fields = fields
}
}
module.exports = {
APIError,
UnauthorizedError,
NotFoundError,
ForbiddenError,
BadRequestError
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment