我们在写ABAP代码时,出要返回错误消息,我们一般写
1 2 |
MESSAGE '条码内容为空!' TYPE 'E'. MESSAGE '条码内容为空!' TYPE 'I'. |
之类的错误代码返回错误消息,但在ODATA的类中,如直接这样写的话,那在ODATA的返回只会说是在/IWFND/ERROR_LOG 中可查询错误,错误如下
1 2 3 4 5 6 7 8 9 10 11 12 |
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <code>0050569259751EE4BA9710043F8A5115</code> <message xml:lang="zh">In the context of Data Services an unknown internal server error occurred</message> <innererror xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <transactionid>42AB5881078F0090E005EE19D9BDF0BF</transactionid> <timestamp>20200819010457.1021360</timestamp> <Error_Resolution> <SAP_Transaction>For backend administrators: use ADT feed reader "SAP Gateway Error Log" or 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> </Error_Resolution> </innererror> </error> |
而事实上我们想返回的错误是最终用户看的,此满足不了我们的需求,所以需要对返回错误内容进行修改。
原文地址:https://www.saplearners.com/map-message-odata-response-header-sap-message/
1.说明
SAP官方建议的是把错误消息放入OData response header parameter “sap-message”
2.ODATA中错误的写法
1 2 3 4 5 6 7 8 9 10 11 12 13 |
*1. Instatiate the Message Container DATA: lo_message_container TYPE REF TO /iwbep/if_message_container. CALL METHOD me->/iwbep/if_mgw_conv_srv_runtime~get_message_container RECEIVING ro_message_container = lo_message_container. CALL METHOD lo_message_container->add_message EXPORTING iv_msg_type = /iwbep/cl_cos_logger=>warning iv_msg_id = 'ZTEST' iv_msg_number = '000' iv_add_to_response_header = abap_true. "add the message to the header |
3.改进
上面的写法是把错误内容写到HEADER里,但读取时总是有些麻烦,而在WEBIDE中,默认程序模板并不能调用到上面返回的错误信息,使用如下的代码,在SAP WEB IDE中可以默认返回错误内容,但内容信息有点长,也是还需要再进行改进。
1 2 3 4 5 6 7 8 9 10 11 12 |
DATA: lo_message_container TYPE REF TO /iwbep/if_message_container. DATA: l_msgtx TYPE symsgv, l_msg_text TYPE bapi_msg, LS_MESSAGE TYPE SCX_T100KEY. LS_MESSAGE-MSGID = ''. LS_MESSAGE-MSGNO = ''. LS_MESSAGE-ATTR1 = '保存数据失败'. RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION EXPORTING TEXTID = LS_MESSAGE. |
更多相关函数可参看:
ADD_MESSAGE_FROM_EXCEPTION
ADD_MESSAGES_FROM_BAPI
ADD_MESSAGE_FROM_BAPI
ADD_MESSAGE
ADD_MESSAGE_TEXT_ONLY
此时我们测试时会得到如图
4.SAPUI5中使用此ODATA错误消息的用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
sap.ui.define([ "sap/ui/core/mvc/Controller" ], function(Controller) { "use strict"; return Controller.extend("com.saplearners.controller.View1", { onInit: function() { var oModel = new sap.ui.model.json.JSONModel(); this.getView().setModel(oModel, "viewData"); var oServiceModel = new sap.ui.model.odata.v2.ODataModel("/sap/opu/odata/sap/ZMSG_CONT_DEMO_SRV/"); var mParameters = { method: "GET", success: jQuery.proxy(function(oData, response) { this.getView().getModel("viewData").setProperty("/AirlineSet", oData.results); // response header var hdrMessage = response.headers["sap-message"]; var hdrMessageObject = JSON.parse(hdrMessage); // log the header message console.log(hdrMessageObject); console.log(hdrMessageObject.message); }, this), error: jQuery.proxy(function(oError) { }, this) }; oServiceModel.read("/AirlineSet", mParameters); } }); }); |