Skip to content

Commit

Permalink
Merge pull request #98 from trojs/feature/access-error
Browse files Browse the repository at this point in the history
Add the Access error
  • Loading branch information
w3nl authored Nov 27, 2024
2 parents 714327c + f5c74da commit 2b65db3
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Types:

* AppError
* AuthenticationError
* AccessError
* NoContent
* NotFoundError
* NotImplementedError
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand All @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions src/__tests__/access-error.unit.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
8 changes: 4 additions & 4 deletions src/__tests__/app-error.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/authentication-error.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions src/access-error.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -14,6 +15,7 @@ export {
makeAppError,
AppError,
AuthenticationError,
AccessError,
NoContentError,
NotFoundError,
NotImplementedError,
Expand Down

0 comments on commit 2b65db3

Please sign in to comment.