Last active
January 18, 2016 19:29
-
-
Save tshaddix/8554cd1f0f741a375b4a to your computer and use it in GitHub Desktop.
App errors for JS applications that loosely match the specs of JSON API.
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
import util from 'util'; | |
class AppError { | |
constructor(message) { | |
Error.call(this); | |
Error.captureStackTrace(this, this.constructor.name); | |
this.name = this.constructor.name; | |
this.message = message; | |
} | |
} | |
util.inherits(AppError, Error); | |
class HttpError extends AppError { | |
constructor(status, code, title, detail, path) { | |
super(`(${status}) ${title}: ${detail}`); | |
this.status = status; | |
this.code = code; | |
this.title = title; | |
this.detail = detail; | |
this.path = path; | |
} | |
toObject() { | |
const { | |
title, | |
detail, | |
status, | |
code, | |
path | |
} = this; | |
return { | |
title, | |
detail, | |
status, | |
code, | |
path | |
}; | |
} | |
} | |
class NotFoundError extends HttpError { | |
constructor(resource) { | |
super(404, 404, 'Not Found', 'Resource not found', resource); | |
} | |
} | |
class ForbiddenError extends HttpError { | |
constructor(detail) { | |
super(403, 403, 'Forbidden', detail || 'You do not have access to this resource'); | |
} | |
} | |
class UnauthorizedError extends HttpError { | |
constructor(detail) { | |
super(401, 401, 'Unauthorized', detail || 'You must be authorized to access this resource'); | |
} | |
} | |
class InternalServerError extends HttpError { | |
constructor() { | |
super(500, 500, 'Internal Server Error', 'An internal server error occured'); | |
} | |
} | |
class BadRequestError extends HttpError { | |
constructor(detail, path) { | |
super(400, 400, 'Bad Request', detail, path); | |
} | |
} | |
class ConflictError extends HttpError { | |
constructor(resource) { | |
super(409, 409, 'Conflict', 'Resource already exists', resource); | |
} | |
} | |
export { | |
HttpError, | |
AppError, | |
InternalServerError, | |
NotFoundError, | |
UnauthorizedError, | |
ForbiddenError, | |
BadRequestError, | |
ConflictError | |
}; |
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
'use strict'; | |
var util = require('util'); | |
function AppError(status, code, title, detail, path, error) { | |
Error.call(this); | |
Error.captureStackTrace(this, this.constructor); | |
this.name = this.constructor.name; | |
this.message = detail; | |
this.status = status; | |
this.code = code; | |
this.title = title; | |
this.detail = detail; | |
this.path = path; | |
this.error = error; | |
} | |
util.inherits(AppError, Error); | |
function unauthorized(detail) { | |
return new AppError( | |
401, 'UNAUTHORIZED', 'Unauthorized', detail || 'Unauthorized'); | |
} | |
function forbidden(intended) { | |
return new AppError( | |
403, 'FORBIDDEN', 'Forbidden', | |
intended ? 'Forbidden: ' + intended : 'Forbidden'); | |
} | |
function path(p, msg) { | |
return new AppError(400, 'PATH', 'Path', msg, p); | |
} | |
function alreadyExists(error) { | |
return new AppError(409, | |
'CONFLICT', 'Already Exists', | |
'Resource already exists: ', undefined, error); | |
} | |
function internal(error) { | |
return new AppError(500, | |
'INTERNAL', 'Internal Server Error', | |
'An internal server error occurred', undefined, error); | |
} | |
function notFound(resource) { | |
return new AppError(404, 'NOT_FOUND', | |
'Not Found', 'Resource not found: ' + resource); | |
} | |
module.exports = { | |
AppError: AppError, | |
unauthorized: unauthorized, | |
forbidden: forbidden, | |
path: path, | |
alreadyExists: alreadyExists, | |
internal: internal, | |
notFound: notFound | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment