I am using PromiseKit (awesome framework, btw!) to handle communication between my app and a server API. I would like to create some kind of global handler to respond to things like notifying the user about a lack of network connection across any of my many promises.
The PMKPromise.h file lists an unhandled error handler:
/**
Called by PromiseKit in the event of unhandled errors.
The default handler NSLogs the error. Note, your handler is executed
from an undefined queue, unless you manage thread-safe data, dispatch to
a safe queue before doing anything else in your handler.
*/
extern void (^PMKUnhandledErrorHandler)(NSError *);
This type is then implemented in the PMKPromise.m file, though the doc block implies that this could be overwritten. My question is how exactly do I redefine this variable?
Copying the same syntax as used in the .m file gives me a linker error
void(^PMKUnhandledErrorHandler)(id) = ^(NSError *error){
//...
};
==> duplicate symbol _PMKUnhandledErrorHandler
PMKUnhandledErrorHandler = ^(id error) {
// your code here
};
Be wary of the unhandier error handler, sometimes it gets called a noticeable amount of time after the error occurred, depending on when the promise in question is deallocated. It is reliable, but not always immediately reliable.
99% of the time it would be slow because some object has a handle on a promise somewhere in your chain still and it soon to be deallocated but is not yet deallocated.