Skip to content

Instantly share code, notes, and snippets.

@rhoboat
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhoboat/0dd7c9b3d6dd29f187e1 to your computer and use it in GitHub Desktop.
Save rhoboat/0dd7c9b3d6dd29f187e1 to your computer and use it in GitHub Desktop.
custom error class (uses lodash)
'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