Last active
August 29, 2015 14:25
-
-
Save rhoboat/0dd7c9b3d6dd29f187e1 to your computer and use it in GitHub Desktop.
custom error class (uses lodash)
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
'use strict'; | |
import lodash as _ from 'lodash'; | |
/* | |
options can be: | |
- name: The name property represents a name for the type of error. | |
See this for throwing a custom error: | |
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name | |
*/ | |
class WHError extends Error { | |
constructor (message = "Error without a message", | |
inner = null, | |
{ name: "WHError" }) { | |
this.name = name; | |
this.message = message; | |
if (inner) { | |
this.innerError = inner; | |
} | |
this.toString = formatErrorMessage; | |
} | |
} | |
function formatErrorMessage () { | |
let err = this; | |
let messages = [ constructMessage(err) ]; | |
while (err.innerError) { | |
err = err.innerError; | |
messages.push( constructMessage(err) ); | |
} | |
return _.chain(messages) | |
.reverse(messages) | |
.join(' -> ') | |
.value(); | |
} | |
function constructMessage ({ name, message }) { | |
return `${name}: ${message}`; | |
} | |
export default WHError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment