diff --git a/assets/errorMessages.js b/assets/errorMessages.js index a78666a..62c4a08 100644 --- a/assets/errorMessages.js +++ b/assets/errorMessages.js @@ -1,12 +1,13 @@ const errorMessages = { - 400: "The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).", - 401: "The request has not been applied because it lacks valid authentication credentials for the target resource.", - 403: "The server understood the request but refuses to authorize it.", - 404: "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.", - 408: "The server did not receive a complete request message within the time that it was prepared to wait.", - 422: "The server understands the content type of the request entity, and the syntax of the request entity is correct but was unable to process the contained instructions.", - 500: "The server encountered an unexpected condition that prevented it from fulfilling the request.", - 502: "The server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.", -} + 400: 'The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).', + 401: 'The request has not been applied because it lacks valid authentication credentials for the target resource.', + 403: 'The server understood the request but refuses to authorize it.', + 404: 'The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.', + 408: 'The server did not receive a complete request message within the time that it was prepared to wait.', + 418: "I'm a teapot.", + 422: 'The server understands the content type of the request entity, and the syntax of the request entity is correct but was unable to process the contained instructions.', + 500: 'The server encountered an unexpected condition that prevented it from fulfilling the request.', + 502: 'The server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.', +}; module.exports = errorMessages; diff --git a/src/errors/server/IAmATeapot.js b/src/errors/server/IAmATeapot.js new file mode 100644 index 0000000..9a7bb71 --- /dev/null +++ b/src/errors/server/IAmATeapot.js @@ -0,0 +1,20 @@ +const DomainError = require('../DomainError'); +const errorMessages = require('../../../assets/errorMessages'); + +/** + * 418: I'm a teapot + * + * Any attempt to brew coffee with a teapot should result in the error + * code "418 I'm a teapot". The resulting entity body MAY be short and + * stout + * + * Source: RFC2324 Section 2.3.2 + */ + +class IAmATeapot extends DomainError { + constructor(msg = errorMessages[418]) { + super(msg, 418); + } +} + +module.exports = IAmATeapot; diff --git a/src/errors/server/index.js b/src/errors/server/index.js index c097d3e..109d3aa 100644 --- a/src/errors/server/index.js +++ b/src/errors/server/index.js @@ -1,2 +1,3 @@ exports.BadGatewayError = require('./BadGatewayError'); exports.InternalServerError = require('./InternalServerError'); +exports.IAmATeapot = require('./IAmATeapot'); diff --git a/src/index.js b/src/index.js index 9eb6d33..93a76b3 100644 --- a/src/index.js +++ b/src/index.js @@ -15,6 +15,8 @@ const throwError = (statusCode, msg) => { throw new errors.RequestTimeoutError(msg); case 404: throw new errors.ResourceNotFoundError(msg); + case 418: + throw new errors.ImATeapot(msg); case 422: throw new errors.UnprocessableEntityError(msg); case 502: diff --git a/tests/throwError.test.js b/tests/throwError.test.js index 8f3e500..8a5fc52 100644 --- a/tests/throwError.test.js +++ b/tests/throwError.test.js @@ -15,6 +15,9 @@ describe('Testing throwError function', () => { it('Should throw 404 Resource Not Found Error', () => { expect(() => errors.throwError(404)).to.throw(errors.ResourceNotFoundError) }); + it('Should throw 418 I\'m a teapot Entity Error', () => { + expect(() => errors.throwError(418)).to.throw(errors.IAmATeapotError) + }); it('Should throw 422 Unprocessable Entity Error', () => { expect(() => errors.throwError(422)).to.throw(errors.UnprocessableEntityError) });