Raise Odata error message handle in SAPUI5

After an Odata call to the backend server, you would like to return message to SAPUI5 application from SAP Odata method. If the SAP Odata call has failed, then we must return an Odata error message to the SAPUI5 application.

Here we learn how to handle SAP server side message for error scenario in SAPUI5 application. We also learn what will happen if we do not raise an exception in Odata method.

Raise error message in SAP Odata method

Raising an error message from SAP Odata method is 3 step process:

  1. Define a message container object
  2. Add message text to message container object
  3. Export message container object in raised exception

The below code snippet gives a quick an easy way to send an error message from back-end Odata method to front-end sapui5 application. Here we raise an exception of type /iwbep/cx_mgw_busi_exception , a business exception. The other exception that can be raised is /IWBEP/CX_MGW_TECH_EXCEPTION which is a technical exception.

METHOD scarrset_update_entity.

* Update the record
DATA : wa_key_tab LIKE LINE OF it_key_tab,
       wa_airline TYPE zcl_zscarr_mpc=>ts_scarr.

* Read the Key from URI
READ TABLE it_key_tab INTO wa_key_tab WITH KEY name = 'Carrid'.
IF sy-subrc EQ 0.
* Read the data from HTTP Request body
  io_data_provider->read_entry_data(
     IMPORTING
      es_data             = wa_airline
  ).

 MOVE-CORRESPONDING wa_airline TO er_entity.
 UPDATE scarr SET carrname = wa_airline-carrname
                 currcode = wa_airline-currcode
                 url      = wa_airline-url
           WHERE carrid   = wa_key_tab-value.

IF sy-subrc NE 0.
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_text_only
  EXPORTING
    iv_msg_type               =  'E'                
    iv_msg_text               =  'Update is unsuccessful...'  
    .
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
  EXPORTING
    textid = /iwbep/cx_mgw_busi_exception=>business_error
    message_container = lo_message_container
    .
 ENDIF.
ENDIF.
ENDMETHOD.

Handle SAP OData error message in SAPUI5

The below code demonstrates to how to display an Odata error message passed from back-end SAP server in SAPUI5 application.

When ever SAP backend server Odata method raises an excetion, it returns an error object in the SAPUI5 application and the error function gets executed. We fetch the error message from the error object passes from the back-end SAP server.

that.getView().getModel().
	update("/scarrSet('" + oCust1 + "')",
		postData, null,
function(response) {
	MessageToast.show("Upadte successful for " + oCust1);
	},
function(Error) {
var disMsg = JSON.parse(Error.response.body).error.message.value;
MessageToast.show(disMsg + oCust1);
});

If we do not raise an exception in the SAP Odata method, then even if the Odata method has failed, it will return back in the success function in the SAPUI5 application. Hence it is important to raise an exception in the backend Odata method so that the odata call returns back in the error function in SAPUI5. Here we handle the error message appropirately.