javascript - How to differentiate between an unexpected error and planned rejection in angular promises? -
if understand correctly (promises api confusing), error occurred in promise suppressed, following code:
$http.post(...) .then(data => { if (data.nogood) { return $q.reject(); } else { // bug. var = {}; return a.a.a > 3; } }) .catch(error => { console.error('bad data'); });
is buggy, baddata
not true if 'bad data'
logged).
the desired behaviour me see console error upon every real runtime error, , leave handling rejections me. missing here? correct approach?
checking if error error
on each catch
block seems tedious.
unless rejection explicitly constructed error
class or child, rejections can differentiated types.
.catch(error => { if (!(error instanceof error)) console.error(error) });
exceptions $q
promises handled $exceptionhandler
service , logged default, if caught catch
. without error instanceof error
type check console.error(error)
result in logging them twice.
despite reject
, throw
are used interchangeably of times, $q
promises makes sense reject
rejection reasons , throw
errors error
objects keep scheme working.
Comments
Post a Comment