Skip to main content

Module 22: Error Handling & Logging (SAPUI5)

Error handling is not just about catching failures —
it is about guiding users, preserving trust, and making issues diagnosable.

Enterprise SAPUI5 applications must implement centralized, consistent, and user-friendly error handling.


1. Why Centralized Error Handling Matters


Problems with Ad-hoc Error Handling

  • Duplicate code
  • Inconsistent messages
  • Poor user experience
  • Hard-to-debug production issues
Enterprise Rule

Errors should be handled once, centrally, not everywhere.


2. Global Error Handler


What is a Global Error Handler?

A global error handler:

  • Catches unhandled errors
  • Handles OData request failures
  • Centralizes logging & messaging

Typically implemented in:

  • Component.js
  • ErrorHandler.js (utility class)

Attaching Global OData Error Handler

onInit: function () {
this.getModel().attachRequestFailed(this._onRequestFailed, this);
}

Example Global Handler Method

_onRequestFailed: function (oEvent) {
var oResponse = oEvent.getParameter("response");
// Central error processing
}
Best Practice

Keep global error handling out of controllers.


3. OData Error Parsing


Structure of OData Error Response

Typical OData error payload:

{
"error": {
"code": "Z_ERROR",
"message": {
"value": "Invalid sales order"
}
}
}

Parsing OData Error Message

var oError = JSON.parse(oResponse.responseText);
var sMessage = oError.error.message.value;

Handling Multiple Messages

Some responses contain multiple messages (batch responses).

Real Projects

Always expect more than one error message.


4. MessageManager (Very Important)


What is MessageManager?

sap.ui.core.message.MessageManager:

Central repository for messages

Supports errors, warnings, info

Integrates with MessagePopover

SAP Recommendation

Use MessageManager for structured error handling.


Registering MessageManager

var oMessageManager = sap.ui.getCore().getMessageManager();
this.getView().setModel(oMessageManager.getMessageModel(), "messages");

Adding a Message Programmatically

oMessageManager.addMessages(
new sap.ui.core.message.Message({
message: "Amount is invalid",
type: sap.ui.core.MessageType.Error,
target: "/Amount",
processor: this.getView().getModel()
})
);

5. Centralized Error Handling Pattern


Controllers

ErrorHandler (Utility)

MessageManager

UI (MessagePopover / Inline)

Benefits

  • Consistent messages
  • Reusable logic
  • Easy maintenance
  • Better testability
Architecture Tip

Think of error handling as a service, not UI logic.


6. User-Friendly Error Display


Inline Field Errors (Preferred)

Use:

ValueState

MessageManager binding

<Input
value="{/Amount}"
valueState="{messages>/valueState}"
valueStateText="{messages>/message}" />
UX Rule

Show errors near the field, not in popups.


MessagePopover (Multiple Errors)

<MessagePopover
items="{messages>/}">
</MessagePopover>

Use when:

Multiple validation errors

Backend batch failures


MessageBox (Blocking Errors)

sap.m.MessageBox.error("Save failed");

Use only for:

Global failures

Save/submit errors

UX Warning

Overusing MessageBox frustrates users.


7. Logging Strategy (Frontend)


What to Log in UI5

  • Technical errors
  • Failed requests
  • Unexpected states
Best Practice

Log technical details, show business-friendly messages.


Example: Console Logging (Dev Only)

console.error(oError);
Production Rule

Do not expose technical logs to end users.


8. UI5 Error Handling vs Backend Logging

AspectUI5Backend (ABAP)
PurposeUser feedbackDiagnostics
ToolMessageManagerSLG1
VisibilityEnd-userAdmin
Full-Stack Insight

Frontend informs the user, backend logs the truth.


9. Common Error Handling Mistakes

Avoid These
  • Parsing errors in every controller
  • Showing raw backend messages
  • Ignoring batch error responses
  • No central error handler

10. Interview-Grade Explanation

Q: How do you handle errors in SAPUI5 applications?

Answer:

I implement centralized error handling using a global error handler and MessageManager. OData errors are parsed centrally, user-friendly messages are displayed inline or via MessagePopover, and technical details are logged without exposing them to end users.


11. Summary

  • Centralized error handling is mandatory
  • Global handlers catch OData failures
  • MessageManager manages structured messages
  • Inline errors improve UX
  • MessagePopover handles multiple errors
  • Logging and messaging serve different purposes

12. What’s Next?

➡️ Module 23: Security & Authorization in SAPUI5

Learning Tip

Good error handling is invisible — bad error handling is unforgettable.