Skip to content

Instantly share code, notes, and snippets.

@ezmiller
Created January 21, 2015 20:01
Show Gist options
  • Save ezmiller/39ccfa40a1349b1709c3 to your computer and use it in GitHub Desktop.
Save ezmiller/39ccfa40a1349b1709c3 to your computer and use it in GitHub Desktop.
One way to create globally available custom errors as a service in SailsJS
'use strict';
/**
* Indicates an error where an invalid argument or input to a method.
* @param {string} message The error message.
* @param {string} stack The stack trace.
*/
function InvalidArgumentException(message, stack) {
this.name = 'InvalidArgumentException';
this.message = message || 'Invalid Argument';
this.stack = stack;
}
InvalidArgumentException.prototype = new Error();
InvalidArgumentException.prototype.constructor = InvalidArgumentException;
module.exports = {
InvalidArgumentException: InvalidArgumentException
}
@ezmiller
Copy link
Author

You'd place this file in your api/services directory, and then you would the do this later on:

throw new CustomErrors.InvalidArgumentException('Invalid argument', new Error().stack);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment