View on GitHub

guides

Exception Hierarchy

Background

Exceptions are good - they simplify error handling and increase readability. 

Rules

Implementation hints

Have base exceptions you’ll use all over the application

Create exceptions inheriting from these base exceptions to constrain possible exceptions being thrown from services:

try {
    return URLFetchingService.get(uri, params);
} catch (e) {
    if (e instanceof BaseTemporaryErrorException) {
        return retry();
    } else if (e instanceof URLFetchingBadURIException) {
        this.addErrors({uri: ’Wrong URI format.’});
    } else if (e instanceof URLFetchingNotFoundException) {
        this.addErrors({nonfield: ’Resource is long gone.’});
    } else if (e instanceof BasePermanentErrorException) {
        this.onFailure();

        // this will:
        //  - fire up a notification
        //  - notify us if e is BaseCriticalInternalFailureException
        ErrorService.dealWith(e); 
    } else {
        throw e;
    }
}