diff --git a/README.md b/README.md index d9a3ddc..0c1535e 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Types: * AppError * AuthenticationError + * AccessError * NoContent * NotFoundError * NotImplementedError diff --git a/package-lock.json b/package-lock.json index 4da4816..e1a4540 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@trojs/error", - "version": "4.0.1", + "version": "4.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@trojs/error", - "version": "4.0.1", + "version": "4.1.0", "license": "MIT", "dependencies": { "@trojs/validator": "^11.0.0" diff --git a/package.json b/package.json index 1516c2c..9cee498 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@trojs/error", "description": "Extended Errors", - "version": "4.0.1", + "version": "4.1.0", "author": { "name": "Pieter Wigboldus", "url": "https://trojs.org/" @@ -22,6 +22,7 @@ "src/index.js", "src/app-error.js", "src/authentication-error.js", + "src/access-error.js", "src/no-content-error.js", "src/not-found-error.js", "src/not-implemented-error.js", diff --git a/src/__tests__/access-error.unit.js b/src/__tests__/access-error.unit.js new file mode 100644 index 0000000..4566a2b --- /dev/null +++ b/src/__tests__/access-error.unit.js @@ -0,0 +1,49 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert' +import { AccessError } from '../index.js' + +/* eslint-disable sonarjs/no-duplicate-string */ + +describe('Access Error test', () => { + it('It should create an Access error', () => { + const error = new AccessError({ + value: 'test', + type: String, + message: 'Example text' + }) + + assert.deepEqual(error instanceof AccessError, true) + assert.deepEqual(error instanceof TypeError, true) + assert.deepEqual(error instanceof Error, true) + assert.deepEqual(error.name, 'AccessError') + assert.deepEqual(error.message, 'Example text') + assert.deepEqual(error.value, 'test') + assert.deepEqual(error.status, 403) + assert.deepEqual(error.type, String) + assert.deepEqual(error.date.constructor, Date) + assert.deepEqual( error.stack.includes('AccessError: Example text') , true) + }) + + it('It should handle invalid error values', () => { + const error = new AccessError({ + value: 'test', + type: 'string', + message: 'Example text' + }) + + assert.deepEqual(error instanceof AccessError, true) + assert.deepEqual(error instanceof Error, true) + assert.deepEqual(error.name, 'AccessError') + 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, 'AccessError') + assert.deepEqual(error.value.values.status, 403) + 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('AccessError: Invalid error') , true) + }) +}) diff --git a/src/__tests__/app-error.unit.js b/src/__tests__/app-error.unit.js index 5f0101e..968dde0 100644 --- a/src/__tests__/app-error.unit.js +++ b/src/__tests__/app-error.unit.js @@ -5,7 +5,7 @@ import { AppError } from '../index.js' /* eslint-disable sonarjs/no-duplicate-string */ describe('App Error test', () => { - it('It should create a app error with minimum fields', () => { + it('It should create an app error with minimum fields', () => { const error = new AppError({ message: 'Example text' }) @@ -22,7 +22,7 @@ describe('App Error test', () => { assert.deepEqual(error.me, null) }) - it('It should create a app error with null on optional fields', () => { + it('It should create an app error with null on optional fields', () => { const error = new AppError({ value: null, type: null, @@ -42,7 +42,7 @@ describe('App Error test', () => { assert.deepEqual(error.me, null) }) - it('It should create a app error with undefined on optional fields', () => { + it('It should create an app error with undefined on optional fields', () => { const error = new AppError({ value: undefined, type: undefined, @@ -62,7 +62,7 @@ describe('App Error test', () => { assert.deepEqual(error.me, null) }) - it('It should create a app error', () => { + it('It should create an app error', () => { const error = new AppError({ value: 'test', type: String, diff --git a/src/__tests__/authentication-error.unit.js b/src/__tests__/authentication-error.unit.js index 5cd45c3..94cec5b 100644 --- a/src/__tests__/authentication-error.unit.js +++ b/src/__tests__/authentication-error.unit.js @@ -5,7 +5,7 @@ import { AuthenticationError } from '../index.js' /* eslint-disable sonarjs/no-duplicate-string */ describe('Authentication Error test', () => { - it('It should create a authentication error', () => { + it('It should create an authentication error', () => { const error = new AuthenticationError({ value: 'test', type: String, diff --git a/src/access-error.js b/src/access-error.js new file mode 100644 index 0000000..3e554d3 --- /dev/null +++ b/src/access-error.js @@ -0,0 +1,23 @@ +import makeAppError from './app-error.js' + +const AppError = makeAppError(TypeError) + +class AccessError extends AppError { + /** + * Get the error name + * @returns {string} + */ + get name() { + return 'AccessError' + } + + /** + * Get the error status + * @returns {number} + */ + get errorStatus() { + return 403 + } +} + +export default AccessError diff --git a/src/index.js b/src/index.js index d8d3f05..0946640 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import makeAppError from './app-error.js' import AuthenticationError from './authentication-error.js' +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' @@ -14,6 +15,7 @@ export { makeAppError, AppError, AuthenticationError, + AccessError, NoContentError, NotFoundError, NotImplementedError,