Skip to content

Instantly share code, notes, and snippets.

@dpjayasekara
Forked from deepal/wrapping-errors.js
Created October 14, 2019 21:11
Show Gist options
  • Save dpjayasekara/9d77759ba5bfb1bed172ab1db00e98c5 to your computer and use it in GitHub Desktop.
Save dpjayasekara/9d77759ba5bfb1bed172ab1db00e98c5 to your computer and use it in GitHub Desktop.
class UserServiceError extends Error {
constructor(...args) {
super(...args);
this.code = 'ERR_USER_SERVICE';
this.name = 'UserServiceError';
this.stack = `${this.message}\n${new Error().stack}`;
}
}
class InvalidInputError extends Error {
constructor(...args) {
super(...args);
this.code = 'ERR_INVALID_INPUT';
this.name = 'InvalidInputError';
this.stack = `${this.message}\n${new Error().stack}`;
}
}
async function getUser(userId) {
if (!userId) throw new InvalidInputError('userId is not provided');
try {
return getUserFromApi(userId);
} catch (err) {
throw new UserServiceError(err.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment