-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add custom module errors, better error handling, remove dep…
…recated tests
- Loading branch information
1 parent
c4180f2
commit 16b6338
Showing
7 changed files
with
134 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* TrembitaError constructor | ||
* | ||
* @param {String} msg Error message | ||
* @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error | ||
*/ | ||
|
||
function TrembitaError(msg) { | ||
Error.call(this); | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this); | ||
} else { | ||
this.stack = new Error().stack; | ||
} | ||
this.message = msg; | ||
Object.defineProperty(this, 'name', { value: this.constructor.name }) | ||
} | ||
|
||
/*! | ||
* Inherits from Error. | ||
*/ | ||
|
||
TrembitaError.prototype = Object.create(Error.prototype); | ||
TrembitaError.prototype.constructor = Error; | ||
|
||
/*! | ||
* Module exports. | ||
*/ | ||
|
||
module.exports = exports = TrembitaError; | ||
|
||
/*! | ||
* Expose subclasses | ||
*/ | ||
|
||
TrembitaError.UnexpectedStatusCodeError = require('./unexpectedStatusCodeError'); |
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,78 @@ | ||
/*! | ||
* Module requirements | ||
*/ | ||
const util = require('util'); | ||
const TrembitaError = require('./'); | ||
|
||
/** | ||
* Unexpected StatusCode Error | ||
* | ||
* @api private | ||
* @param {Document} instance | ||
* @inherits TrembitaError | ||
*/ | ||
|
||
function UnexpectedStatusCodeError(instance) { | ||
this.message = ''; | ||
|
||
if (instance && instance.constructor.name === 'Object') { | ||
this.message = _generateErrorMessage(instance) | ||
TrembitaError.call(this, this.message); | ||
} else { | ||
this.message = 'Unexpected Status Code Error'; | ||
TrembitaError.call(this, this.message); | ||
} | ||
|
||
this.name = 'UnexpectedStatusCodeError'; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this); | ||
} else { | ||
this.stack = new Error().stack; | ||
} | ||
} | ||
|
||
/*! | ||
* Inherits from TrembitaError. | ||
*/ | ||
|
||
UnexpectedStatusCodeError.prototype = Object.create(TrembitaError.prototype); | ||
UnexpectedStatusCodeError.prototype.constructor = TrembitaError; | ||
|
||
/** | ||
* Console.log helper | ||
*/ | ||
|
||
UnexpectedStatusCodeError.prototype.toString = function() { | ||
return this.name + ': ' + _generateErrorMessage(this); | ||
}; | ||
|
||
/*! | ||
* inspect helper | ||
*/ | ||
|
||
UnexpectedStatusCodeError.prototype.inspect = function() { | ||
return Object.assign(new Error(this.message), this); | ||
}; | ||
|
||
/*! | ||
* Helper for JSON.stringify | ||
*/ | ||
|
||
UnexpectedStatusCodeError.prototype.toJSON = function() { | ||
return Object.assign({}, this, { message: this.message }); | ||
}; | ||
|
||
/*! | ||
* ignore | ||
*/ | ||
|
||
function _generateErrorMessage (instance) { | ||
return `${`Unexpected status code: ${instance.httpStatusCode}, Body: ${util.inspect(instance.httpBody, { depth: 4 })}, Options: `}${util.inspect(instance.options, { depth: 4 })}` | ||
} | ||
|
||
|
||
/*! | ||
* Module exports | ||
*/ | ||
|
||
module.exports = exports = UnexpectedStatusCodeError; |
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"lint": "eslint *.js", | ||
"lint": "eslint index.js", | ||
"ci": "npm run lint && npm run test" | ||
}, | ||
"author": "Oleg Koval <[email protected]> (http://wearereasonablepeople.com/)", | ||
|
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 was deleted.
Oops, something went wrong.