-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(httperrors): add support for http errors 4xx and 5xx
- Loading branch information
Showing
62 changed files
with
2,601 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @module Messages/AbstractError | ||
* @desc Export the AbstractError abstract class. | ||
* | ||
* @requires {@link https://github.com/Itee/itee-validators itee-validators} | ||
* @requires {@link https://github.com/uuidjs/uuid uuid} | ||
* | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
import { | ||
isBlankString, | ||
isEmptyString, | ||
isNotDefined, | ||
isNotString | ||
} from 'itee-validators' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
/** | ||
* @class | ||
* @classdesc The AbstractError is the base class for all derived errors. | ||
* It is composed by an uuid v4, the name which is based on the instance constructor name, and a message | ||
* | ||
* @extends Error | ||
*/ | ||
class AbstractError extends Error { | ||
|
||
/** | ||
* A boolean based on classname that allow fast type checking, will ever be true | ||
* @constant | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
get isAbstractError () { return true } | ||
|
||
/** | ||
* An auto-generated universally unique identifier, this allow to recognize any error by id | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
get uuid () { return this._uuid } | ||
|
||
set uuid ( value ) { throw new SyntaxError( 'Try to assign a read only property.' ) } | ||
|
||
/** | ||
* The name of current instanced error (a.k.a the constructor name) | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
get name () { return this._name } | ||
|
||
set name ( value ) { throw new SyntaxError( 'Try to assign a read only property.' ) } | ||
|
||
/** | ||
* The error message | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
get message () { return this._message } | ||
|
||
set message ( value ) { throw new SyntaxError( 'Try to assign a read only property.' ) } | ||
|
||
/** | ||
* @constructor | ||
* @param message {string} The error message to dispatch | ||
*/ | ||
constructor ( message ) { | ||
super() | ||
|
||
this._uuid = uuidv4() | ||
this._name = this.constructor.name | ||
this._message = ( () => { | ||
// Validate message before assign it as readonly property ! | ||
const expect = 'Expect a non empty string.' | ||
if ( isNotDefined( message ) ) { throw new ReferenceError( `The error message cannot be null or undefined. ${ expect }` )} | ||
if ( isNotString( message ) ) { throw new TypeError( `The error message cannot be an instance of ${ message.constructor.name }. ${ expect }` )} | ||
if ( isEmptyString( message ) ) { throw new TypeError( `The error message cannot be an empty string. ${ expect }` )} | ||
if ( isBlankString( message ) ) { throw new TypeError( `The error message cannot be a blank string. ${ expect }` )} | ||
|
||
return message | ||
} )() | ||
|
||
// Override the default Error stack behavior and apply get/set to avoid mutation | ||
this._stack = this.stack | ||
|
||
/** | ||
* The stack trace of the error | ||
* @member module:Messages/AbstractError~AbstractError#stack | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
Object.defineProperty( this, 'stack', { | ||
get: () => { return this._stack }, | ||
set: () => { throw new SyntaxError( 'Try to assign a read only property.' ) } | ||
} ) | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
export { AbstractError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
|
||
export * from './AbstractError' | ||
export * from './http/_http' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @module Messages/HTTP/ATimeoutOccuredError | ||
* @desc Export the ATimeoutOccuredError http message class. | ||
* | ||
* @requires {@link https://github.com/Itee/itee-validators itee-validators} | ||
* @requires module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
* | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
|
||
import { AbstractHTTPError } from './AbstractHTTPError' | ||
|
||
/** | ||
* @class | ||
* @classdesc The UnprocessableEntityError is the error class for this kind of error. | ||
* It extend is AbstractHTTPError and fix his status code. | ||
* | ||
* @extends module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
*/ | ||
class ATimeoutOccuredError extends AbstractHTTPError { | ||
|
||
/** | ||
* A boolean based on classname that allow fast type checking, will ever be true | ||
* @constant | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
get isATimeoutOccuredError () { return true } | ||
|
||
/** | ||
* The static statusCode getter reimplementation for this kind of error, will return 524 | ||
* @see module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError#statusCode | ||
* @static | ||
* @constant | ||
* @default 422 | ||
* @type {number} | ||
*/ | ||
static get statusCode () { return 524 } | ||
|
||
} | ||
|
||
export { ATimeoutOccuredError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @module Messages/HTTP/AbstractHTTPError | ||
* @desc Export the AbstractHTTPError abstract class. | ||
* | ||
* @requires {@link https://github.com/Itee/itee-validators itee-validators} | ||
* @requires module:Messages/AbstractError~AbstractError | ||
* | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
|
||
import { isNotDefined } from 'itee-validators' | ||
import { AbstractError } from '../AbstractError' | ||
|
||
/** | ||
* @class | ||
* @classdesc The AbstractHTTPError is the base class for all derived HTTPError. | ||
* It extend is AbstractError and agmente it with the status code notion. | ||
* | ||
* @extends module:Messages/AbstractError~AbstractError | ||
*/ | ||
class AbstractHTTPError extends AbstractError { | ||
|
||
/** | ||
* A boolean based on classname that allow fast type checking, will ever be true | ||
* @constant | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
static get isAbstractHTTPError () { return true } | ||
|
||
/** | ||
* The abstract getter of http status code, internally it call the static getter statusCode that need to be reimplemented by extended class. | ||
* @readonly | ||
* @abstract | ||
* @type {number} | ||
* @throws {ReferenceError} In case the static statusCode getter is not redefined in class that inherit this class. | ||
*/ | ||
get statusCode () { | ||
if ( isNotDefined( this.constructor.statusCode ) ) { | ||
throw new ReferenceError( `${ this.name } class need to reimplement static statusCode getter.` ) | ||
} | ||
return this.constructor.statusCode | ||
} | ||
|
||
set statusCode ( value ) { | ||
throw new SyntaxError( 'Try to assign a read only property.' ) | ||
} | ||
} | ||
|
||
export { AbstractHTTPError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @module Messages/HTTP/BadGatewayError | ||
* @desc Export the BadGatewayError http message class. | ||
* | ||
* @requires {@link https://github.com/Itee/itee-validators itee-validators} | ||
* @requires module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
* | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
|
||
import { AbstractHTTPError } from './AbstractHTTPError' | ||
|
||
/** | ||
* @class | ||
* @classdesc The UnprocessableEntityError is the error class for this kind of error. | ||
* It extend is AbstractHTTPError and fix his status code. | ||
* | ||
* @extends module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
*/ | ||
class BadGatewayError extends AbstractHTTPError { | ||
|
||
/** | ||
* A boolean based on classname that allow fast type checking, will ever be true | ||
* @constant | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
get isBadGatewayError () { return true } | ||
|
||
/** | ||
* The static statusCode getter reimplementation for this kind of error, will return 502 | ||
* @see module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError#statusCode | ||
* @static | ||
* @constant | ||
* @default 422 | ||
* @type {number} | ||
*/ | ||
static get statusCode () { return 502 } | ||
|
||
} | ||
|
||
export { BadGatewayError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @module Messages/HTTP/BadMappingError | ||
* @desc Export the BadMappingError http message class. | ||
* | ||
* @requires {@link https://github.com/Itee/itee-validators itee-validators} | ||
* @requires module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
* | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
|
||
import { AbstractHTTPError } from 'AbstractHTTPError' | ||
|
||
/** | ||
* @class | ||
* @classdesc The UnprocessableEntityError is the error class for this kind of error. | ||
* It extend is AbstractHTTPError and fix his status code. | ||
* | ||
* @extends module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
*/ | ||
class BadMappingError extends AbstractHTTPError { | ||
|
||
/** | ||
* A boolean based on classname that allow fast type checking, will ever be true | ||
* @constant | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
get isBadMappingError () { return true } | ||
|
||
/** | ||
* The static statusCode getter reimplementation for this kind of error, will return 421 | ||
* @see module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError#statusCode | ||
* @static | ||
* @constant | ||
* @default 422 | ||
* @type {number} | ||
*/ | ||
static get statusCode () { return 421 } | ||
|
||
} | ||
|
||
export { BadMappingError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @module Messages/HTTP/BadRequestError | ||
* @desc Export the BadRequestError http message class. | ||
* | ||
* @requires {@link https://github.com/Itee/itee-validators itee-validators} | ||
* @requires module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
* | ||
* @author [Tristan Valcke]{@link https://github.com/Itee} | ||
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause} | ||
*/ | ||
|
||
import { AbstractHTTPError } from './AbstractHTTPError' | ||
|
||
/** | ||
* @class | ||
* @classdesc The UnprocessableEntityError is the error class for this kind of error. | ||
* It extend is AbstractHTTPError and fix his status code. | ||
* | ||
* @extends module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError | ||
*/ | ||
class BadRequestError extends AbstractHTTPError { | ||
|
||
/** | ||
* A boolean based on classname that allow fast type checking, will ever be true | ||
* @constant | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
get isBadRequestError () { return true } | ||
|
||
/** | ||
* The static statusCode getter reimplementation for this kind of error, will return 400 | ||
* @see module:Messages/HTTP/AbstractHTTPError~AbstractHTTPError#statusCode | ||
* @static | ||
* @constant | ||
* @default 422 | ||
* @type {number} | ||
*/ | ||
static get statusCode () { return 400 } | ||
|
||
} | ||
|
||
export { BadRequestError } |
Oops, something went wrong.