Friday 15 May 2015

javascript - Handling multiple catches in promise chain -



javascript - Handling multiple catches in promise chain -

i still new promises , using bluebird currently, have scenario not quite sure how best deal it.

so illustration have promise chain within express app so:

repository.query(getaccountbyidquery) .catch(function(error){ res.status(404).send({ error: "no business relationship found id" }); }) .then(convertdocumenttomodel) .then(verifyoldpassword) .catch(function(error) { res.status(406).send({ oldpassword: error }); }) .then(changepassword) .then(function(){ res.status(200).send(); }) .catch(function(error){ console.log(error); res.status(500).send({ error: "unable alter password" }); });

so behaviour after is:

goes business relationship id if there rejection @ point, bomb out , homecoming error if there no error convert document returned model verify password database document if passwords dont match bomb out , homecoming different error if there no error alter passwords then homecoming success if else went wrong, homecoming 500

so catches not seem stop chaining, , makes sense, wondering if there way me somehow forcefulness chain stop @ point based upon errors, or if there improve way construction form of branching behaviour, there case of if x y else z.

any help great.

this behavior synchronous throw:

try{ throw new error(); } catch(e){ // handle } // code run, since recovered error!

that's half of point of .catch - able recover errors. might desirable rethrow signal state still error:

try{ throw new error(); } catch(e){ // handle throw e; // or wrapper on e know wasn't handled } // code not run

however, lone won't work in case since error caught later handler. real issue here generalized "handle anything" error handlers bad practice in general , extremely frowned upon in other programming languages , ecosystems. reason bluebird offers typed , predicate catches.

the added advantage business logic not (and shouldn't) have aware of request/response cycle @ all. not query's responsibility decide http status , error client gets , later app grows might want separate business logic (how query db , how process data) send client (what http status code, text , response).

here how i'd write code.

first, i'd .query throw nosuchaccounterror, i'd subclass promise.operationalerror bluebird provides. if you're unsure how subclass error allow me know.

i'd additionally subclass authenticationerror , like:

function changepassword(querydataetc){ homecoming repository.query(getaccountbyidquery) .then(convertdocumenttomodel) .then(verifyoldpassword) .then(changepassword); }

as can see - it's clean , can read text instruction manual of happens in process. separated request/response.

now, i'd phone call route handler such:

changepassword(params). catch(nosuchaccounterror, function(e){ res.status(404).send({ error: "no business relationship found id" }); }).catch(authenticationerror, function(e){ res.status(406).send({ oldpassword: error }); }).error(function(e){ // catches remaining operational errors res.status(500).send({ error: "unable alter password" }); }).catch(function(e){ res.status(500).send({ error: "unknown internal server error" }); });

this way, logic in 1 place , decision of how handle errors client in 1 place , don't clutter eachother.

javascript node.js promise bluebird

No comments:

Post a Comment