Skip to content

Instantly share code, notes, and snippets.

@ivanberry
Forked from spion/translate-error.js
Created October 8, 2018 13:10
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 ivanberry/e6ea59f1dbe2f3c04b419c3ae36948cf to your computer and use it in GitHub Desktop.
Save ivanberry/e6ea59f1dbe2f3c04b419c3ae36948cf to your computer and use it in GitHub Desktop.
function translateError(msg) {
var newErr = new Error(msg); // placed here to get correct stack
return e => {
newErr.originalError = e;
throw newErr;
}
}
async function asyncTask() {
const user = await UserModel.findById(1).catch(translateError('No user found'))
if(!user) throw new Error('No user found');
const savedTask = await TaskModel({userId: user.id, name: 'Demo Task'})
.catch(translateError('Error occurred while saving task'))
if(user.notificationsEnabled) {
await NotificationService.sendNotification(user.id, 'Task Created')
.catch(translateError('Error while sending notification'))
}
if(savedTask.assignedUser.id !== user.id) {
await NotificationService.sendNotification(savedTask.assignedUser.id, 'Task was created for you')
.catch(translateError('Error while sending notification'))
}
return savedTask
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment