javascript - acceptable promise pattern for 'LOUD' errors? -
i'm using rsvp library distributed inside ember.js , i'm trying figure out correct pattern reporting fatal errors inside promise -- particularly want inform of that's result of programming error due api misuse , want in loud way. i'm new promises , javascript in general, question makes sense
here's simple example (in coffeescript):
doasync = (arg, callback) -> throw new error('you gave me way bad arg, fail you!') promisereturningapi = (data) -> return new ember.rsvp.promise (resolve, reject) -> callback = (err, resp) -> if err reject(err) else resolve(resp) doasync(data, callback)
now lets i've identified error there's no possible way recover occurred inside doasync -- want make sure error gets reported if caller neglected attach error handler because resulted because caller invoked api function in incorrect way
i came across idea of using settimeout within rejection handler ensure error gets raised somewhere if caller doesn't attach error handler promise
failloud = (err) -> if err.isprogrammererror settimeout () -> throw err throw err promisereturningapi = (data) -> promise = new ember.rsvp.promise (resolve, reject) -> callback = (err, resp) -> if(err) reject(err) else resolve(resp) doasync(data, callback) return promise.then(null, failloud)
is considered reasonable practice attach such default error handler promise before returning promisereturningapi? allow me force stacktrace when caller can't possibly work -- though stacktrace little odd make things bit easier started ...
even though called example promise returning function 'api' call -- should add i'm not writing framework code -- rather within application. if doasync real-world function, in versio of real-world pretty coming external party new-to-me api -- seems pretty i'll misuse while i'm getting know it... meaning might want make pattern this
failloud = (err) -> if err?.isprogrammererror settimeout () -> throw err throw err promisereturningapi = (data) -> promise = new ember.rsvp.promise (resolve, reject) -> callback = (err, resp) -> if(err) reject(err) resolve(resp) try doasync(data, callback) catch err err.isprogrammererror = true throw err return promise.then(null, failloud)
i think doing forcing exception thrown somewhere time asynchronous function call invocation raises exception -- such exception raised during argument validation phase of async call commonly going result of application code passing in doesn't make sense -- , want find out can. seem reasonable pattern follow aid in debugging promises used in application code in context?
new answer --
in video panel discussion ember core developers, @ 1 point developers share 1 debugging tip:
http://www.youtube.com/watch?v=l9oomygo1hi
tom dale addresses issue of swallowed exceptions inside promises , recommends use of new ember.rsvp.onerror feature debugging errors inside promises have otherwise gone unreported because no rejection handler attached.
i think correct answer question -- although don't yet know how use rsvp.onerror callback (or in ember releases available) ...
Comments
Post a Comment