add common HTTP errors

This commit is contained in:
Tim Alby 2019-07-26 18:14:56 +02:00
parent 0dd8b124bd
commit 9229c9b3a9
4 changed files with 111 additions and 2 deletions

89
libraries/o-error/http.js Normal file
View file

@ -0,0 +1,89 @@
const OError = require('./index')
class HttpError extends OError {
constructor(options) {
super(options)
this.statusCode = options.statusCode || 500
}
}
class InternalServerError extends HttpError {
constructor(options) {
super({ message: 'Internal Server Error', statusCode: 500, ...options })
}
}
class ServiceUnavailableError extends HttpError {
constructor(options) {
super({ message: 'Service Unavailable', statusCode: 503, ...options })
}
}
class BadRequestError extends HttpError {
constructor(options) {
super({ message: 'Bad Request', statusCode: 400, ...options })
}
}
class UnauthorizedError extends HttpError {
constructor(options) {
super({ message: 'Unauthorized', statusCode: 401, ...options })
}
}
class ForbiddenError extends HttpError {
constructor(options) {
super({ message: 'Forbidden', statusCode: 403, ...options })
}
}
class NotFoundError extends HttpError {
constructor(options) {
super({ message: 'Not Found', statusCode: 404, ...options })
}
}
class MethodNotAllowedError extends HttpError {
constructor(options) {
super({ message: 'Method Not Allowed', statusCode: 405, ...options })
}
}
class NotAcceptableError extends HttpError {
constructor(options) {
super({ message: 'Not Acceptable', statusCode: 406, ...options })
}
}
class ConflictError extends HttpError {
constructor(options) {
super({ message: 'Conflict', statusCode: 409, ...options })
}
}
class UnprocessableEntityError extends HttpError {
constructor(options) {
super({ message: 'Unprocessable Entity', statusCode: 422, ...options })
}
}
class TooManyRequestsError extends HttpError {
constructor(options) {
super({ message: 'Too Many Requests', statusCode: 429, ...options })
}
}
module.exports = {
HttpError,
InternalServerError,
ServiceUnavailableError,
BadRequestError,
UnauthorizedError,
ForbiddenError,
NotFoundError,
MethodNotAllowedError,
NotAcceptableError,
ConflictError,
UnprocessableEntityError,
TooManyRequestsError
}

View file

@ -1,6 +1,6 @@
{ {
"name": "@overleaf/o-error", "name": "@overleaf/o-error",
"version": "2.0.0", "version": "2.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View file

@ -1,6 +1,6 @@
{ {
"name": "@overleaf/o-error", "name": "@overleaf/o-error",
"version": "2.0.0", "version": "2.1.0",
"description": "Make custom error types that pass `instanceof` checks, have stack traces, support custom messages and properties, and can wrap causes (like VError).", "description": "Make custom error types that pass `instanceof` checks, have stack traces, support custom messages and properties, and can wrap causes (like VError).",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View file

@ -0,0 +1,20 @@
const OError = require('..')
const HttpErrors = require('../http')
describe('OError/http', () => {
it('is instance of OError', () => {
try {
throw new HttpErrors.ConflictError()
} catch (e) {
expect(e).to.be.instanceof(OError)
}
})
it('has status code', () => {
try {
throw new HttpErrors.ConflictError()
} catch (e) {
expect(e.statusCode).to.equal(409)
}
})
})