5. Error Handling

For web frontends, error handling is one of the most important topics, especially due to the large number of objects. Anything can go wrong at any time.

Ideally, you need to handle two types of errors:

  1. Something failed on the frontend (e.g., a throw new Error('message') somewhere)
  2. Something failed on the backend (BUSI_EXCEPTION/any other system error/dump during request processing)

To avoid duplicating error handling code every time, it is HIGHLY RECOMMENDED to move it to a utility class and call it as needed.

So, instead of a bulky construction like this:

this.getView().setBusy(true);
this.getModel().read("/Entity('key'", {
    ///.......
    error: function () {
        sap.m.MessageBox.error("Error!", {
            styleClass: this.getOwnerComponent().getContentDensityClass()  
        });
        this.getView().setBusy(false);
    },
    success: function(oData){
        // Processing, which can also potentially throw errors
        // And here you’ll also have to do
        // try { } catch (e) { }
        // callback hell expected here
    }
});

can be replaced with a more minimalistic and clear approach:

this.getView().setBusy(false);
try{
    const entity = await this.useOdata().read("/Entity('key')");
    // Processing, which can also potentially throw errors
    // throw new Error("An error occurred while processing X");
} catch(e) {
    this.showMessage(e); // A method in the parent class for parsing oData errors

} finally {
    this.getView().setBusy(false);
}

In general, you can just leave a single line for error handling.
If you need to handle something for business logic, you can catch the specific error right here.

For unification, I suggest a class with an error parser and a BaseController class with explicit methods for displaying errors to the user.

Use the right control

  1. Informational and success messages — can be shown via Message Toast
  2. Error and warning messages should be shown explicitly to the user — in a popup dialog (MessageBox or Dialog). For displaying multiple errors, use MessageView.

(If you use BaseController from my repository, you already have this in the showMessage() method)