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
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.jsErrorHandler.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
}
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).
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
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
Recommended Architecture
Controllers
↓
ErrorHandler (Utility)
↓
MessageManager
↓
UI (MessagePopover / Inline)
Benefits
- Consistent messages
- Reusable logic
- Easy maintenance
- Better testability
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}" />
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
Overusing MessageBox frustrates users.
7. Logging Strategy (Frontend)
What to Log in UI5
- Technical errors
- Failed requests
- Unexpected states
Log technical details, show business-friendly messages.
Example: Console Logging (Dev Only)
console.error(oError);
Do not expose technical logs to end users.
8. UI5 Error Handling vs Backend Logging
| Aspect | UI5 | Backend (ABAP) |
|---|---|---|
| Purpose | User feedback | Diagnostics |
| Tool | MessageManager | SLG1 |
| Visibility | End-user | Admin |
Frontend informs the user, backend logs the truth.
9. Common Error Handling Mistakes
- 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
Good error handling is invisible — bad error handling is unforgettable.