cancel
Showing results for 
Search instead for 
Did you mean: 

Exception message handling in sapui5 ?

Former Member
0 Kudos

Hi all,

I have an input field in the frontend which gets some records and displays them .Now if the respective record is not found due to some issue or if bapi is failed i want to raise the message in the frontend .

if i just raise the below exception code in my method in sap gateway is it going to take care of message handling or i have do anything in the font end too?


  raise exception type /iwbep/cx_mgw_busi_exception

      exporting

       textid = /iwbep/cx_mgw_busi_exception=>business_error

       message = message.

Thanks for support help me out..

I searched the prior articles and found the the above code i could not find if anything needs to be done in frontend apart from backend exception call.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

thanks

when add the following exception call in get_stream method i am getting this message which i had set from backend in the console and error_log

exception call made in backedn


raise exception type /iwbep/cx_mgw_busi_exception

       exporting

        textid = /iwbep/cx_mgw_busi_exception=>business_error

        message = 'message frm sap'.

console message

gateway error log

But its not getting displayed in the screen popup? what i found is

(err.response.body).error.message.value) is triggering syntax error.I tried to get the response

var obj = JSON.parse(err.response.body); but at this point the syntax error is triggered.

Error triggered is "ncaught SyntaxError: Unexpected token <" at this line

var obj = JSON.parse(err.response.body);

How do i over come it?

former_member182372
Active Contributor
0 Kudos

what's in err.response.body? must be XML

    try {

      // Try to parse as a JSON string

      oMessage = JSON.parse(oError);

    } catch (err) {

      try {

        // Whoops - not JSON, check if this is XML

        switch (typeof oError) {

          case "string": // XML or simple text

            if (oError.indexOf("<?xml") == 0) {

              var oXML = jQuery.parseXML(oError);

              var oXMLMsg = oXML.querySelector("message");

              if (oXMLMsg) {

                messageText = oXMLMsg.textContent;

              }

            } else {

              // Nope just return the string

              messageText = oError;

            }

            break;

         

          case "object": // Exception

            messageText = oError.toString();

            break;

        }

      } catch (err) {

        messageText = "An unknown error occurred";

      }

Former Member
0 Kudos

Thanks a lot

Former Member
0 Kudos

Hi Maksim ,

Can you try to solve my other doubt posted in the following url .It is similar to error handling and filters

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi tried calling the get_stream method in gateway in INit method of a view and tried raising exception.

The debugger is not executing the error method.after the oproductmodel.read the debugger goes out of init method. after call both odata and err is undefined

I am i missing something?

former_member182372
Active Contributor
0 Kudos

try to throw /IWBEP/CX_MGW_TECH_EXCEPTION so code will be 500 and error call back function will be executed

Former Member
0 Kudos

Hi,

This is code in controller

Please correct if i am wrong

I have placed the debugger in the get_Stream gateway method.The break point is hit when the above code in controller is executed.But still why does it goes to error function though the call to service was a success ?

But still why does my message text in err object doesnot change to what i passed from backend

former_member182372
Active Contributor
0 Kudos

Check what is in your err.response.body

try {

      // Try to parse as a JSON string

      var oMessage = JSON.parse(err.response.body);

    } catch (err) {

}

scott_stefanich
Active Participant
0 Kudos

Hello Deepan,

From the OData Read API Documentation ,

An OData model read operation (HTTP GET) has optional parameters including success and error callback functions. The error callback function can have the parameter oError which contains additional error information.


As an example, here is an OData Read with the optional parameter map:



this.getModel().read("/InvalidPath",{

  context: {},

  urlParamters: {},

  async: true,

  filters: [],

  sorters: [],

  success: function(){

  },

  error: function(oError){

    console.log(oError);

  }

});


The oError object's message, request, and response can be examined in the console. JSON.parse can be to parse the error's message body string as JSON for further handling, e.g. an error message in a sap.m.MessageBox.


Regards,

Scott

Former Member
0 Kudos

Hi thanks,

I using the following code


var sRead = "/formset(orderno='" + sordno + "')"+"/$value";

       

            oModel.read( sRead, null, null, true, function(oData, oResponse){

              

                 $('#detailPage--frame').attr('src',oResponse.requestUri);

                 debugger;  

              

            },function(err) {

                //Error Callback:

                alert("Error occurred " + err.message)});

           }

here err.message will have the error text passed from backedn right?

This is how i passed the message from backend

can you show how to add the message my code snippet?

when i use error: function() i am getting syntax error "Syntax error on token ":"" .sorry for too basic question i am in process of learning.

scott_stefanich
Active Participant
0 Kudos

From back-end to front-end development, we certainly have a lot of opportunities for learning!

Try this (no promises),


this.getModel().read("/InvalidPath", {

    context: {},

    urlParameters: {},

    async: true,

    filters: [],

  sorters: [],

    success: function(oData, oResponse){

      $('#detailPage--frame').attr('src',oResponse.requestUri);

      debugger;

    },

    error: function(err) {

     //Error Callback

   jQuery.sap.require("sap.m.MessageBox");

      sap.m.MessageBox.show((JSON.parse(err.response.body).error.message.value));

    }

});

Regards,

Scott

SergioG_TX
Active Contributor
0 Kudos

Deepan,

I concur with Scott answer. Even though you may be able to pass those params to the read function without having a    property: value , I'd suggest to do it like Scott's example as it will be more readable.

If you continue getting a syntax error on the front-end, open the browser developer tools (F12) and set a breakpoint in the error callback and use the console window to see the error object you are getting in order to determine its properties.