mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 04:13:41 -05:00
add common HTTP errors
This commit is contained in:
parent
0dd8b124bd
commit
9229c9b3a9
4 changed files with 111 additions and 2 deletions
89
libraries/o-error/http.js
Normal file
89
libraries/o-error/http.js
Normal 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
|
||||
}
|
2
libraries/o-error/package-lock.json
generated
2
libraries/o-error/package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@overleaf/o-error",
|
||||
"version": "2.0.0",
|
||||
"version": "2.1.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -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": {
|
||||
|
|
20
libraries/o-error/test/http.test.js
Normal file
20
libraries/o-error/test/http.test.js
Normal 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)
|
||||
}
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue