Skip to content

Commit

Permalink
Merge pull request #20 from hckrnews/feature/type-async-function
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
w3nl authored Feb 1, 2022
2 parents a894e49 + 868b6e8 commit 4caade3
Show file tree
Hide file tree
Showing 10 changed files with 1,953 additions and 2,067 deletions.
3,890 changes: 1,824 additions & 2,066 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/error",
"description": "Extended Errors",
"version": "0.3.6",
"version": "0.3.7",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down
58 changes: 58 additions & 0 deletions src/app-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@ import errorSchema from './schemas/error.js';

const validator = new Validator(errorSchema);

/**
* @typedef {Object} ErrorValues
* @property {string} name
* @property {string} message
* @property {any} value
* @property {number} status
* @property {object} type
* @property {Date} date
* @property {object} me
*/

export default (error = Error) =>
class AppError extends error {
/**
* Set the error values
*
* @param {object} error
* @param {any} error.value
* @param {object} error.type
* @param {string} error.nessage
* @param {object} error.me
*/
constructor({ value = null, type = null, message, me = null }) {
super(message);

Expand All @@ -16,18 +36,41 @@ export default (error = Error) =>
this.setValues({ value, type, me });
}

/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'AppError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 500;
}

/**
* Get the error status, or 500 if the error is invalid
*
* @return {number}
*/
get status() {
return this.hasErrors ? 500 : this.errorStatus;
}

/**
* Set the error values
*
* @param {object} data
* @param {any} data.value
* @param {object} data.type
* @param {object} data.me
*/
setValues({ value, type, me }) {
this.value = value;
this.type = type;
Expand All @@ -41,10 +84,20 @@ export default (error = Error) =>
}
}

/**
* Check if the error has errors
*
* @return {boolean}
*/
get hasErrors() {
return validator.errors.length > 0;
}

/**
* Get the error values
*
* @return {ErrorValues}
*/
get values() {
return {
name: this.name,
Expand All @@ -57,6 +110,11 @@ export default (error = Error) =>
};
}

/**
* Validate the error values
*
* @return {boolean}
*/
validate() {
return validator.validate(this.values);
}
Expand Down
10 changes: 10 additions & 0 deletions src/authentication-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError(TypeError);

class AuthenticationError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'AuthenticationError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 401;
}
Expand Down
10 changes: 10 additions & 0 deletions src/no-content-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError(RangeError);

class NoContentError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'NoContentError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 204;
}
Expand Down
10 changes: 10 additions & 0 deletions src/not-found-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError(RangeError);

class NotFoundError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'NotFoundError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 404;
}
Expand Down
10 changes: 10 additions & 0 deletions src/not-implemented-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError(RangeError);

class NotImplementedError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'NotImplementedError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 405;
}
Expand Down
10 changes: 10 additions & 0 deletions src/server-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError();

class ServerError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'ServerError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 500;
}
Expand Down
10 changes: 10 additions & 0 deletions src/timeout-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError();

class TimeoutError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'TimeoutError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 408;
}
Expand Down
10 changes: 10 additions & 0 deletions src/validation-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import makeAppError from './app-error.js';
const AppError = makeAppError(TypeError);

class ValidationError extends AppError {
/**
* Get the error name
*
* @return {string}
*/
get name() {
return 'ValidationError';
}

/**
* Get the error status
*
* @return {number}
*/
get errorStatus() {
return 400;
}
Expand Down

0 comments on commit 4caade3

Please sign in to comment.