From 42dc540ff7f2a85debc8810c33263436b3a4d684 Mon Sep 17 00:00:00 2001 From: Pieter Wigboldus Date: Wed, 27 Nov 2024 11:07:09 +0100 Subject: [PATCH] Add the processing error --- README.md | 21 +++++------ package-lock.json | 4 +-- package.json | 3 +- src/__tests__/processing-error.unit.js | 49 ++++++++++++++++++++++++++ src/index.js | 2 ++ src/processing-error.js | 23 ++++++++++++ 6 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 src/__tests__/processing-error.unit.js create mode 100644 src/processing-error.js diff --git a/README.md b/README.md index 0c1535e..256dfa1 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,17 @@ The object extends an Error object. Types: - * AppError - * AuthenticationError - * AccessError - * NoContent - * NotFoundError - * NotImplementedError - * ServerError - * TimeoutError - * ValidationError - * RateLimitError + * NoContent (204) + * ValidationError (400) + * AuthenticationError (401) + * AccessError (403) + * NotFoundError (404) + * NotImplementedError (405) + * TimeoutError (408) + * ProcessingError (422) + * RateLimitError (429) + * AppError (500) + * ServerError (500) You can send: diff --git a/package-lock.json b/package-lock.json index e1a4540..da2ca3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@trojs/error", - "version": "4.1.0", + "version": "4.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@trojs/error", - "version": "4.1.0", + "version": "4.2.0", "license": "MIT", "dependencies": { "@trojs/validator": "^11.0.0" diff --git a/package.json b/package.json index 9cee498..5271cf8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@trojs/error", "description": "Extended Errors", - "version": "4.1.0", + "version": "4.2.0", "author": { "name": "Pieter Wigboldus", "url": "https://trojs.org/" @@ -26,6 +26,7 @@ "src/no-content-error.js", "src/not-found-error.js", "src/not-implemented-error.js", + "src/processing-error.js", "src/server-error.js", "src/timeout-error.js", "src/validation-error.js", diff --git a/src/__tests__/processing-error.unit.js b/src/__tests__/processing-error.unit.js new file mode 100644 index 0000000..dadda8f --- /dev/null +++ b/src/__tests__/processing-error.unit.js @@ -0,0 +1,49 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert' +import { ProcessingError } from '../index.js' + +/* eslint-disable sonarjs/no-duplicate-string */ + +describe('Processing Error test', () => { + it('It should create a Processing error', () => { + const error = new ProcessingError({ + value: 'test', + type: String, + message: 'Example text' + }) + + assert.deepEqual(error instanceof ProcessingError, true) + assert.deepEqual(error instanceof TypeError, true) + assert.deepEqual(error instanceof Error, true) + assert.deepEqual(error.name, 'ProcessingError') + assert.deepEqual(error.message, 'Example text') + assert.deepEqual(error.value, 'test') + assert.deepEqual(error.status, 422) + assert.deepEqual(error.type, String) + assert.deepEqual(error.date.constructor, Date) + assert.deepEqual( error.stack.includes('ProcessingError: Example text') , true) + }) + + it('It should handle invalid error values', () => { + const error = new ProcessingError({ + value: 'test', + type: 'string', + message: 'Example text' + }) + + assert.deepEqual(error instanceof ProcessingError, true) + assert.deepEqual(error instanceof Error, true) + assert.deepEqual(error.name, 'ProcessingError') + assert.deepEqual(error.message, 'Invalid error') + assert.deepEqual(error.value.errors[0][0], 'type?') + assert.deepEqual(error.value.values.message, 'Invalid error') + assert.deepEqual(error.value.values.name, 'ProcessingError') + assert.deepEqual(error.value.values.status, 422) + assert.deepEqual(error.value.values.type, Error) + assert.deepEqual(error.value.values.value, 'test') + assert.deepEqual(error.status, 500) + assert.deepEqual(error.type, Error) + assert.deepEqual(error.date.constructor, Date) + assert.deepEqual( error.stack.includes('ProcessingError: Invalid error') , true) + }) +}) diff --git a/src/index.js b/src/index.js index 0946640..70159f5 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ import AccessError from './access-error.js' import NoContentError from './no-content-error.js' import NotFoundError from './not-found-error.js' import NotImplementedError from './not-implemented-error.js' +import ProcessingError from './processing-error.js' import ServerError from './server-error.js' import TimeoutError from './timeout-error.js' import ValidationError from './validation-error.js' @@ -19,6 +20,7 @@ export { NoContentError, NotFoundError, NotImplementedError, + ProcessingError, ServerError, TimeoutError, ValidationError, diff --git a/src/processing-error.js b/src/processing-error.js new file mode 100644 index 0000000..886a665 --- /dev/null +++ b/src/processing-error.js @@ -0,0 +1,23 @@ +import makeAppError from './app-error.js' + +const AppError = makeAppError(TypeError) + +class ProcessingError extends AppError { + /** + * Get the error name + * @returns {string} + */ + get name() { + return 'ProcessingError' + } + + /** + * Get the error status + * @returns {number} + */ + get errorStatus() { + return 422 + } +} + +export default ProcessingError