Skip to content

Instantly share code, notes, and snippets.

@AlexDisler
Last active March 29, 2016 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDisler/93d11ba4ea787fdb067e to your computer and use it in GitHub Desktop.
Save AlexDisler/93d11ba4ea787fdb067e to your computer and use it in GitHub Desktop.
class RowNotFound extends Error {
constructor() {
const message = `Row not found`;
super(message);
this.message = message;
this.name = 'RowNotFound';
}
}
function checkRowExists(row) {
if (!row) return Promise.reject(new RowNotFound())
return row;
}
function update(_id) {
return User
.findOneAndUpdate({ _id }, { $set: { updated: true } }, { new: true })
.exec()
.then(checkRowExists)
.then(sendToView)
}
// If you reject the promise with an error instead of a string, you can see the stacktrace (to see what triggered it) and check what kind of error it is
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject
// http://www.devthought.com/2011/12/22/a-string-is-not-an-error/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment