cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 how to display errors sent from OData v4 service?

junweitoh
Associate
Associate
0 Kudos

In my SAPUI5 application, I have a custom function that changes a property of my entity (using context.setProperty) and sends a PATCH request to the backend OData v4 service using submitBatch. I want to handle the scenario when the PATCH request fails, such as if I have sent the wrong data to the backend. I am expecting a 400 response with a JSON response such as

{ "error": { "message": "Value must be greater than 0", "target": "Quantity", "@com.sap.vocabularies.Common.v1.additionalTargets": ["ProductID"], "@Org.OData.Core.V1.ContentID":"0.0" } }

Currently, I am using the @sap-ux/ui5-middleware-fe-mockserver library to mock my server, and have used their API to mock a 400 error (following the sample found here: https://github.com/SAP/open-ux-odata/blob/main/samples/error-handling/webapp/localService/mockdata/R....

My question is - how can I access or retrieve this error response? Currently, the PATCH does not go through (correctly returning 400 response) but in my code, I am unable to catch the error anywhere. Checking `context.hasPendingChanges()` returns false, and surrounding the submitBatch call with a try-catch does not reach the catch block.

View Entire Topic
0 Kudos

Hi Jun Wei Toh,

I would generally do this to read the message from the OData Calls. Register the message manager and attach to a view with json model ("message") in this example.

protected _fnRegisterMessageManager() {
        const oView = this.getView() as View;
        const oMessageManager = sap.ui.getCore().getMessageManager() as MessageManager;
        oMessageManager.registerObject(oView,true);
        const oModel = sap.ui.getCore().getMessageManager().getMessageModel();
        this.getView()?.setModel(oModel,"message");
    }

After the OData Call, in the event patchRequestCompleted/ requestCompleted event of the oData model, this message model should have collected the error messages. You can read the messages from there.

Regards,

Arun K

junweitoh
Associate
Associate
0 Kudos

Hi Arun,

Thanks for the answer. ODatav4 Model does not support the patchRequestCompleted/ requestCompleted events. Which is why I am not able to detect when messages come in, I think

0 Kudos

Hi Jun,

I worte this logic yesterday after triggering the patch call. Can you try within these method to check if you are getting messages.

From the context that you are updating you need to get the binding. and that binding has the patchCompleted event.

https://sapui5.hana.ondemand.com/#/api/sap.ui.model.odata.v4.ODataContextBinding%23events/patchCompl...

You can try either of these.

context.getBinding().attachPatchCompleted(() => { context.refresh(); // check from the message model the messages });

This is one of my old code where i read the message after the patch call.


oBinding.attachEventOnce("patchCompleted", (oEvent) => { var bSuccess = oEvent.getParameter("success"); if (bSuccess) { this.setNotBusy(); oTable.getBinding("items").refresh(); MessageBox.success(`Selected records were updated successfully`); } else { let aMessages = this.getModel("message").getData(); let oErrorMessage = aMessages.find((message) => message.type === 'Error'); this.setNotBusy(); if (oErrorMessage) { oErrorMessage && MessageBox.error(oErrorMessage.message); return; } } });
junweitoh
Associate
Associate
0 Kudos

Hi Arun,

Sorry for not mentioning this earlier, but my scenario is as such: I have a list report page, and in my table I am able to multi-select items. I have a couple of buttons that can perform a PATCH request on the selected items; one such button is 'Approve' that would change the `status` of these table items to `APPROVED`. I am now trying to catch any errors with these `Approve` actions and display them to let the user know that certain items they have selected are unable to be approved (due to network errors, user errors, etc).

Since I have a list, doing `context.getBinding` is tricky as the table or my view itself has no context, only the items in the list. I have tried doing

context.getBinding().attachPatchCompleted(() => { let aMessages = this.getModel("message").getData(); });

but the message array is always empty.

May I know if you have tried doing this for tables before? Thank you!