Skip to Content
avatar image
Former Member

sapui 5 exception handling

Hey,

i am currently writing my first program using sapui5.
i have already implemented all CRUD functions.
my question:
in my CREATE function i am calling another function to check the input. now i want to implement EXCEPTIONS into my programm. how do i get the EXCEPTIONS thrown at the backend to my frontend page. so as an example:
i want to create an entry, but this entry already exists in my database.
so my CREATE_ENTITY function would throw an exception.
how do i show this exception in the frontend. currently this is my call to the create function:

 oModel.create('/RoomsSet', oNewRoom, { success : function(oData, oResponse) { 

sap.m.MessageToast.show("Room succesfully created! Reload Page to see changes"); 
sap.ui.getCore().byId("Popup").destroy(); 
},

error : function(oError) { 

sap.m.MessageToast .show("There was an Error creating the room");

 } 

});

Thank you for your help. Best Regards, Martin
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • May 17, 2017 at 06:48 AM

    If your OData service is returning any exception/error messages then those would be available within the oError parameter of the error callback function. You can check by setting a breakpoint in the error callback function in Chrome's developer console within the Source tab and check what values are retrieved in the oError parameter.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      I just used this code.

      RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception<br>  EXPORTING<br>      textid = /iwbep/cx_mgw_busi_exception=>business_error_unlimited<br>       message_unlimited = 'Your message text'.

      So if i make a wrong call to the Backend ( for example using a room_id that already exists). it says "There was an Error creating the room" and in the chrome debugger is see my error message: 'Your message text'.

      Is there a way to show 'Your message text' inside my MessageToast ?


      Edit: i found a work around:
      i was looking through the error message in the console and found this workaround.

      messageText = oError.responseText.split('message')[3];
      	sap.m.MessageToast.show(messageText);						

      Thank you for your input.

  • avatar image
    Former Member
    May 17, 2017 at 09:57 AM

    Hi,

    I assume you are using the ODataModel (https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.odata.ODataModel.html#create). This is a JavaScript-Promise, so you need to handle it a bit different:

     oModel.create('/RoomsSet', oNewRoom)
    .then(function(oData, oResponse) {
    sap.m.MessageToast.show("Room succesfully created! Reload Page to see changes");
    sap.ui.getCore().byId("Popup").destroy();
    })
    .catch(function(oError) {
    sap.m.MessageToast.show("There was an Error creating the room");
    });
    If you want to read more about Promises: https://www.promisejs.org/
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 18, 2017 at 07:48 PM

    Put a breakpoint or use console.log(oError). In most case error will be under node message.

    var oXML = jQuery.parseXML(oError.response.body);
    var oXMLMsg = oXML.querySelector("message");
    MessageBox.error(oXMLMsg.textContent);

    Add comment
    10|10000 characters needed characters exceeded