From 127f7d6cb7679e5ba458e2e1f45c9b6b463c62d8 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Sat, 7 Nov 2020 20:45:11 +0100 Subject: [PATCH] fix: export 406 and rename 413 (#448) * Export NotAcceptable error type * Rename RequestEntityTooLarge error type --- src/framework/types.ts | 6 +++--- src/index.ts | 6 ++++-- src/middlewares/openapi.multipart.ts | 2 +- src/openapi.validator.ts | 3 ++- test/httperror.spec.ts | 14 +++++++------- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/framework/types.ts b/src/framework/types.ts index 6b5c53b3..90366c87 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -521,7 +521,7 @@ export class HttpError extends Error implements ValidationError { }): | InternalServerError | UnsupportedMediaType - | RequestEntityToLarge + | RequestEntityTooLarge | BadRequest | MethodNotAllowed | NotAcceptable @@ -542,7 +542,7 @@ export class HttpError extends Error implements ValidationError { case 406: return new NotAcceptable(err); case 413: - return new RequestEntityToLarge(err); + return new RequestEntityTooLarge(err); case 415: return new UnsupportedMediaType(err); default: @@ -613,7 +613,7 @@ export class BadRequest extends HttpError { } } -export class RequestEntityToLarge extends HttpError { +export class RequestEntityTooLarge extends HttpError { constructor(err: { path: string; message?: string; diff --git a/src/index.ts b/src/index.ts index a34e95b5..fa391aa1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,9 +5,10 @@ import { OpenApiSpecLoader } from './framework/openapi.spec.loader'; import { InternalServerError, UnsupportedMediaType, - RequestEntityToLarge, + RequestEntityTooLarge, BadRequest, MethodNotAllowed, + NotAcceptable, NotFound, Unauthorized, Forbidden, @@ -19,9 +20,10 @@ export const middleware = openapiValidator; export const error = { InternalServerError, UnsupportedMediaType, - RequestEntityToLarge, + RequestEntityTooLarge, BadRequest, MethodNotAllowed, + NotAcceptable, NotFound, Unauthorized, Forbidden, diff --git a/src/middlewares/openapi.multipart.ts b/src/middlewares/openapi.multipart.ts index e04ad977..ffe4c1dc 100644 --- a/src/middlewares/openapi.multipart.ts +++ b/src/middlewares/openapi.multipart.ts @@ -125,7 +125,7 @@ function error(req: OpenApiRequest, err: Error): ValidationError { message: err.message, }); /*return payload_too_big - ? new RequestEntityToLarge({ path: req.path, message: err.message }) + ? new RequestEntityTooLarge({ path: req.path, message: err.message }) : !unexpected ? new BadRequest({ path: req.path, message: err.message }) : new InternalServerError({ path: req.path, message: err.message });*/ diff --git a/src/openapi.validator.ts b/src/openapi.validator.ts index e5be043b..34cf86ce 100644 --- a/src/openapi.validator.ts +++ b/src/openapi.validator.ts @@ -24,9 +24,10 @@ export { OpenApiValidatorOpts, InternalServerError, UnsupportedMediaType, - RequestEntityToLarge, + RequestEntityTooLarge, BadRequest, MethodNotAllowed, + NotAcceptable, NotFound, Unauthorized, Forbidden, diff --git a/test/httperror.spec.ts b/test/httperror.spec.ts index 1ed6ad18..6b87d652 100644 --- a/test/httperror.spec.ts +++ b/test/httperror.spec.ts @@ -62,21 +62,21 @@ describe(packageJson.name, () => { done(); }); - it('should be an instance of RequestEntityToLarge', (done) => { - console.log('Testing instaceof detection of RequestEntityToLarge'); + it('should be an instance of RequestEntityTooLarge', (done) => { + console.log('Testing instaceof detection of RequestEntityTooLarge'); const err = { - path: '/entity_to_large', - message: 'request qntity too large', + path: '/entity_too_large', + message: 'request entity too large', }; - expect(new error.RequestEntityToLarge(err)).to.be.an.instanceof( - error.RequestEntityToLarge, + expect(new error.RequestEntityTooLarge(err)).to.be.an.instanceof( + error.RequestEntityTooLarge, ); expect( HttpError.create({ status: 413, ...err, }), - ).to.be.an.instanceof(error.RequestEntityToLarge); + ).to.be.an.instanceof(error.RequestEntityTooLarge); done(); });