Skip to Content

SAP UI5: How to cover Backend is down (HTTP 500 Error)

Hi community,

Scenario: Central Hub Gateway

In my UI5-App - is there any way to cover, if the Backend is down? We use ODATA-Model V2. When the Backend is down, I see no chance to handle the HTTP 500 Error.

  • I see in the console after starting the UI5 application, that the $metadata-call is running into a timeout.(No hint for the user)
  • When the ODATA-Service should be called ( like e.g.
oModel.read(..)

the Error-Function is not called.

Unfortunatly, a

try{ 
oModel.read(..)

}catch(oError){

}

does not work as well.

Any ideas?

Thanks in advance!

michael

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Oct 19, 2016 at 07:55 AM

    Hello,

    you can attach two event to your model, and response can be parsed and message text from response can be shown to user. and in your case request failed event will be triggered.

    oModel.attachMetadataFailed(function(oEvent) {
        var oParams = oEvent.getParameters();
        this._showRquestError(oParams.response);
    }, this);
    

    oModel.attachRequestFailed(function(oEvent) {

    var oParams = oEvent.getParameters();

    this._showRquestError(oParams.response);

    }, this); _showRquestError: function(sDetails) { var oErrorDetails = this._parseError(sDetails); var sMsgText = oErrorDetails.sMessage; var sDetails = oErrorDetails.sDetails; // show message box with above message text and details }, _parseError: function(oParameter) { var sMsgText = "", sResponseText = "", oError = {}; sResponseText = oParameter.responseText; // can check for particular status code using oParameter.statusCode i.e. 500 // extract message text from sResponseText, for example as below var oJsonError = JSON.parse(sResponseText);

    sMsgText = oJsonError.error.message.value; //message text from response

    oError.sDetails = sResponseText; oError.sMessage = sMsgText; return oError; }

    Hope this will be helpful.

    Regards,

    Akhilesh

    Add comment
    10|10000 characters needed characters exceeded

    • pls find formatted code below, I am facing lots of issues with editor:(

      oModel.attachMetadataFailed(function(oEvent) {
          var oParams = oEvent.getParameters();
          this._showRquestError(oParams.response);
      }, this);
      
      oModel.attachRequestFailed(function(oEvent) {
          var oParams = oEvent.getParameters();
          this._showRquestError(oParams.response);
      }, this);
      
      _showRquestError: function(sDetails) {
          var oErrorDetails = this._parseError(sDetails);
          var sMsgText = oErrorDetails.sMessage;
          var sDetails = oErrorDetails.sDetails; // show message box with above message text and details 
      },
      _parseError: function(oParameter) {
          var sMsgText = "",
              sResponseText = "",
              oError = {};
          sResponseText = oParameter.responseText; 
      
          // can check for particular status code using oParameter.statusCode i.e. 500
          // extract message text from sResponseText, for example as below
      
          var oJsonError = JSON.parse(sResponseText);
          sMsgText = oJsonError.error.message.value; //message text from response
          oError.sDetails = sResponseText;
          oError.sMessage = sMsgText;
          return oError;
      }

      :)

  • Oct 19, 2016 at 02:58 PM

    oModel.read(..{success:fn,error:fn})

    did u use those callback function? error function should get called in your case.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 20, 2016 at 04:15 AM

    The solution provided by Akilesh does work. I only had to change the Message Parsing.

    I implemented in the ViewController onInit-event the following:

    var oDataModel = this.getOwnerComponent().getModel(); //default ODATA-Service
    oDataModel.attachMetadataFailed(function(oEvent) {
        var sStatusCode = oEvent.getParameter("statusCode");
        var sStatusText = oEvent.getParameter("statusText");
        var sStatusMessage = oEvent.getParameter("message");
        var sMessage = sStatusText + " " + sStatusCode + " : " + sStatusMessage;
        sap.m.MessageBox.show( sMessage, sap.m.MessageBox.Icon.ERROR, "Error"  );
    }, this);
    			
    oDataModel.attachRequestFailed(function(oEvent) {
        var sStatusCode = oEvent.getParameter("statusCode");
        var sStatusText = oEvent.getParameter("statusText");
        var sStatusMessage = oEvent.getParameter("message");
        var sMessage = sStatusText + " " + sStatusCode + " : " + sStatusMessage;
        sap.m.MessageBox.show( sMessage, sap.m.MessageBox.Icon.ERROR, "Error"  );
    }, this);	 
    

    I also tried to bring it into onInit of the BaseController (super-Class of View Controller), but this seems not work or I missed something.

    Thanks!

    michael

    Add comment
    10|10000 characters needed characters exceeded

    • don't attach in any controller file and all, because before control reach to your attached events there might have triggered some request already, so just attach these events in component file itself where setting this model.

      i.e.

      coponent.js:

      this.setModel(oModel);

      //now here attach those events