From 9229c9b3a9ba596de9b214e44484b4fef8758ca6 Mon Sep 17 00:00:00 2001 From: Tim Alby Date: Fri, 26 Jul 2019 18:14:56 +0200 Subject: [PATCH] add common HTTP errors --- libraries/o-error/http.js | 89 +++++++++++++++++++++++++++++ libraries/o-error/package-lock.json | 2 +- libraries/o-error/package.json | 2 +- libraries/o-error/test/http.test.js | 20 +++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 libraries/o-error/http.js create mode 100644 libraries/o-error/test/http.test.js diff --git a/libraries/o-error/http.js b/libraries/o-error/http.js new file mode 100644 index 0000000000..21b65b5d79 --- /dev/null +++ b/libraries/o-error/http.js @@ -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 +} diff --git a/libraries/o-error/package-lock.json b/libraries/o-error/package-lock.json index 9324f8f8e8..a95cce00ce 100644 --- a/libraries/o-error/package-lock.json +++ b/libraries/o-error/package-lock.json @@ -1,6 +1,6 @@ { "name": "@overleaf/o-error", - "version": "2.0.0", + "version": "2.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/libraries/o-error/package.json b/libraries/o-error/package.json index b9d6d59f2b..f61f7e05ff 100644 --- a/libraries/o-error/package.json +++ b/libraries/o-error/package.json @@ -1,6 +1,6 @@ { "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).", "main": "index.js", "scripts": { diff --git a/libraries/o-error/test/http.test.js b/libraries/o-error/test/http.test.js new file mode 100644 index 0000000000..e6c16a68d4 --- /dev/null +++ b/libraries/o-error/test/http.test.js @@ -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) + } + }) +})