How to recover a error message from oData response [SAPUI5]

Carlos Muñoz picture Carlos Muñoz · Feb 24, 2017 · Viewed 13.7k times · Source

I had this issue and i searched a lot how to fix it, but i can't find any solution at the moment...

well, the issue is the next error message, i can write the error but i need a specific entry of this batch.

Error, Messagebox and Batch response.

the code showed on the message box:

{ "message": "HTTP request failed", "headers": { "Content-Type": "application/xml;charset=utf-8", "Content-Length": "1333", "DataServiceVersion": "1.0" }, "statusCode": "400", "statusText": "Bad Request", "responseText": "<?xml version=\"1.0\" encoding=\"utf-8\"?><error xmlns=\"http://schemas .microsoft.com/ado/2007/08/dataservices/metadata\"><code>SY/530</code><message xml:lang=\"es\">No posee permisos para el Centro seleccionado</message><innererror><application><component_id/><service_namespace>/SAP/</service_namespace><service_id>ZQMGW_LECTURATANQUE_SRV</service_id><service_version>0001</service_version></application><transactionid>9488BBDEFA9E11E685950000705EE2FB</transactionid><timestamp>20170224144147.5230000</timestamp><Error_Resolution><SAP_Transaction>Run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details</SAP_Transaction><SAP_Note>See SAP Note 1797736 for error analysis (https: //service. sap .com/sap/support/notes/1797736)</SAP_Note><Batch_SAP_Note>See SAP Note 1869434 for details about working with $batch (https: //service. sap. com/sap/support/notes/1869434)</Batch_SAP_Note></Error_Resolution><errordetails><errordetail><code/><message>No posee permisos para el Centro seleccionado</message><propertyref/><severity>error</severity><target/></errordetail><errordetail><code>/IWBEP/CX_SD_GEN_DPC_BUSINS</code><message>No posee permisos para el Centro seleccionado</message><propertyref/><severity>error</severity><target/></errordetail></errordetails></innererror></error>" }

i need to recover the message tag only, but i don't know how....

the code what i'm using is the native error handling for Sapui5 Fiori Apps:

    constructor: function(oComponent) {
        this._oResourceBundle = oComponent.getModel("i18n").getResourceBundle();
        this._oComponent = oComponent;
        this._oModel = oComponent.getModel();
        this._bMessageOpen = false;
        this._sErrorText = this._oResourceBundle.getText("errorText");

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

        this._oModel.attachRequestFailed(function(oEvent) {
            var oParams = oEvent.getParameters("message");
            // An entity that was not found in the service is also throwing a 404 error in oData.
            // We already cover this case with a notFound target so we skip it here.
            // A request that cannot be sent to the server is a technical error that we have to handle though
            if (oParams.response.statusCode !== "404" || (oParams.response.statusCode === 404 && oParams.response.responseText.indexOf(
                    "Cannot POST") === 0)) {
                this._showServiceError(oParams.response);
            }
        }, this);
    },

    /**
     * Shows a {@link sap.m.MessageBox} when a service call has failed.
     * Only the first error message will be display.
     * @param {string} sDetails a technical error to be displayed on request
     * @private
     */
    _showServiceError: function(sDetails) {
        if (this._bMessageOpen) {
            return;
        }
        this._bMessageOpen = true;
        MessageBox.error(
            this._sErrorText, {
                id: "serviceErrorMessageBox",
                details: sDetails, 
                styleClass: this._oComponent.getContentDensityClass(),
                actions: [MessageBox.Action.CLOSE],
                onClose: function() {
                    this._bMessageOpen = false;
                }.bind(this)
            }
        );
    }

if someone knows how to recover that value, I'll be very greatful.

Greetings.

Answer

Carlos Mu&#241;oz picture Carlos Muñoz · Feb 24, 2017

i fixed this issue, changing this part of the code

if (oParams.response.statusCode !== "404" || (oParams.response.statusCode === 404 && oParams.response.responseText.indexOf(
                "Cannot POST") === 0)) {
            this._showServiceError(oParams.response);
        }
    }, this);

to

if (oParams.response.statusCode !== "404" || (oParams.response.statusCode === 404 && oParams.response.responseText.indexOf(
                    "Cannot POST") === 0)) {
                this._showServiceError($(oParams.response.responseText).find("message").first().text());

            }
        }, this);