如何从 oData 响应中恢复错误消息 [SAPUI5]

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

我遇到了这个问题,我搜索了很多如何解决它,但我目前找不到任何解决方案...

好吧,问题是下一条错误消息,我可以写下错误,但我需要这批的特定条目。

消息框显示的代码:

{ "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>" }

我只需要恢复消息标签,但我不知道如何....

我使用的代码是 Sapui5 Fiori 应用程序的本机错误处理:

    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)
            }
        );
    }

如果有人知道如何恢复该值,我将非常感激。

你好。

检查您的 SAP ABAP 存储库中是否有 HCM_LRQ_CRE BSP 应用程序,实际上它是一个 HCM Leave Request Fiori 应用程序。您可以在那里找到 DataManager-dbg.js 文件。查看 parseErrorMessages 方法,它可以很好地解析 SAP 消息。也许您可以将其用作起点。

我解决了这个问题,更改了这部分代码

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

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);