Skip to content

Commit

Permalink
feat(httperrors): add support for http errors 4xx and 5xx
Browse files Browse the repository at this point in the history
  • Loading branch information
Itee committed Oct 28, 2020
1 parent b23927f commit 73fee38
Show file tree
Hide file tree
Showing 62 changed files with 2,601 additions and 2 deletions.
1 change: 1 addition & 0 deletions configs/rollup.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function CreateRollupConfigs ( options ) {
'buffer',
'fs',
'stream',
'crypto',

'itee-validators',
'itee-utils'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
},
"dependencies": {
"itee-utils": "^5.1.1",
"itee-validators": "^5.1.2"
"itee-validators": "^5.1.2",
"uuid": "^8.3.1"
},
"devDependencies": {
"@semantic-release/changelog": "^5.0.0",
Expand Down
3 changes: 2 additions & 1 deletion sources/itee-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export * from './converters/TAbstractFileConverter'
export * from './databases/TAbstractDatabase'
export * from './databases/TAbstractResponder'


// Messages
export * from './messages/_messages'

// Plugins interfaces
export * from './plugins/TAbstractDatabasePlugin'
102 changes: 102 additions & 0 deletions sources/messages/AbstractError.js
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 }
7 changes: 7 additions & 0 deletions sources/messages/_messages.js
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'
43 changes: 43 additions & 0 deletions sources/messages/http/ATimeoutOccuredError.js
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 }
51 changes: 51 additions & 0 deletions sources/messages/http/AbstractHTTPError.js
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 }
43 changes: 43 additions & 0 deletions sources/messages/http/BadGatewayError.js
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 }
43 changes: 43 additions & 0 deletions sources/messages/http/BadMappingError.js
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 }
43 changes: 43 additions & 0 deletions sources/messages/http/BadRequestError.js
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 }
Loading

0 comments on commit 73fee38

Please sign in to comment.