neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

Check error type on a save #561

Closed nodesocket closed 8 years ago

nodesocket commented 8 years ago

Is there a better way to check the error type that comes back on a save()? Currently I am doing:

project.save((error, doc) => {
    if(error) {
        if(error.name === 'ValidationError') {
            ...
        }
    }
});
MarkHerhold commented 8 years ago

I use instanceof to check against error types, which has been working well.

const thinky = new Thinky(/* settings */);
try {
    const document = yield Model.save();
} catch (err) {
    if (err instanceof thinky.Errors.DocumentNotFound) {
        // handle error
    }
}
nodesocket commented 8 years ago

Ahh perfect, thanks.

if(error instanceof thinky.Errors.ValidationError) {
    ...
}
ondreian commented 8 years ago

you can also use filtered Promise.catch statements

query
  .then(handleSuccess)
  .catch(Errors.DocumentNotFound, handleDoesntExist)
  .catch( err => /*unexpected error, like network partition */ )