When working with submitChanges
all collected changes will be submitted by the sap.ui.model.odata.v2.ODataModel
and processed by the backend.
Updating five different records will result in a response in the success function with atleast five responses. May one or more responses will contain errors. Below is a code snippet showing how to process errors in batch responses.
Function
📟 _processErrors
processing of errors if there are any. the function does not take care of reading out the innererror or the error details.
📟 _processErrorDetails
processing of the innererror/errordetails if there are any. only if processing of the innererror/errordetails fails, the function takes the leading error message.
/** * @memberOf Controller * @description Process the batchResponse and check if there are any errors * function does not care about innererror/errordetails * @param {object} oData - Response from the OData Service */ _processErrors: function(oData) { var aBatchResponses = oData.__batchResponses, oEntitySetResponse = aBatchResponses.pop(), //last response is loading the EntitySet (when submitting update changes) aErrorMessages = []; try { aBatchResponses.forEach(function(oBatchResponse) { var oError = JSON.parse(oBatchResponse.response.body).error; aErrorMessages.push({ Code: oError.code, Message: oError.message.value }); }); } finally { return aErrorMessages; } }, /** * @memberOf Controller * @description Process the batchResponse and check if there are any errors * function cares about innererror/errordetails * @param {object} oData - Response from the OData Service */ _processErrorDetails: function(oData) { var aBatchResponses = oData.__batchResponses, oEntitySetResponse = aBatchResponses.pop(), //last response is loading the EntitySet (when submitting update changes) aErrorMessages = []; try { aBatchResponses.forEach(function(oBatchResponse) { try { var aErrorDetails = JSON.parse(oBatchResponse.response.body).error.innererror.errordetails; aErrorDetails.forEach(function(oError) { aErrorMessages.push({ Code: oError.code, Message: oError.message, Target: oError.target }); }); } catch (oException) { var oError = JSON.parse(oBatchResponse.response.body).error; aErrorMessages.push({ Code: oError.code, Message: oError.message.value }); } }); } finally { return aErrorMessages; } }
Example
🔎 Best Practice
Outsource the following modules via
sap.ui.define
:
/** * @memberOf Controller * @description submitChanges and process errors via {@link Controller._processErrors} or {@link Controller._processErrorDetails} */ save: function() { var oServiceModel = this.getOwnerComponent().getModel, oi18n = this.getResourceBundle(); //only works with BaseController.js if (oServiceModel.hasPendingChanges()) { oServiceModel.submitChanges({ groupId: "xyz", success: function(oData) { var aErrors = this._processErrors(oData); //var aErrors = this._processErrorDetails(oData); if (aErrors.length > 0) { //TODO display the errors somewhere } }.bind(this), error: function(oErr) { //busi exceptions in batch are handled as success //only dumps/technical errors are handled as error sap.m.MessageBox.error(oi18n.getText("errorText"), { details: oErr.responseText }); } }); } }