'ABAP RESTful Programming Model Behaviours - How do I implement error handling?

Currently I'm implementing an unmanaged CREATE behaviour, but the error handling is too generic at the moment..

TRY 

...
 DATA(request) = client_proxy->create_resource_for_entity_set( 'A_SALESORDERITEM' )->create_request_for_create( ).

APPEND 'SALESORDER'            TO so_item_properties.
APPEND 'MATERIAL'              TO so_item_properties.
APPEND 'REQUESTEDQUANTITY'     TO so_item_properties.
APPEND 'REQUESTEDQUANTITYUNIT' TO so_item_properties.
...
          request->set_business_data(
            is_business_data    = items
            it_provided_property = so_item_properties
          ).

...
CATCH /iwbep/cx_gateway.
        APPEND VALUE #(
          symsg-msgty      = 'E'
          symsg-msgid      = '/N4C02/CM_HTTP_COMM'
          symsg-msgno      = '006'
          severity         = if_abap_behv_message=>severity-error
          cid              = sales_order_item_out-cid
        ) TO messages.

        APPEND VALUE #(
            %cid    = sales_order_item_out-cid
            %create = if_abap_behv=>mk-on
        ) TO failed-salesorderitem.
    
...
ENDTRY.

The above merely throws a generic error for all gateway errors which is reported in the Fiori Elements app.

How do I specifically handle each and every Gateway exception caught in iwbep/cx_gateway ?

E.g. the Gateway is current throwing an exception of

No language-specific unit defined in language EN for internal unit T  

... This is for entering a non-existent Quantity unit of T.

The issue is the end-user doesn't see this.... All they get is the generic 'Gateway Exception' error.

I know I can catch this info using something like...

CATCH /iwbep/cx_gateway INTO DATA(lx_gateway).

However, I have a number of issues with what is being return in lx_gateway above.

Firstly, accessing it is problematic as it's an object with attached methods to retrieve data (e.g. get_text( ) ).

Secondly, the data format looks to depend on the error, here are some scenarios I've tested.

  1. Everything correct apart from an invalid RequestedQuantityUnit.

    500 HTTP status

    MSGID BM

    MSGNO 302

    MSGV1 pce

    MSGV2 EN

    MSGV3

    MSGV4

  2. Everything correct apart from a blank Material.

400 HTTP status

Unlike the above error, no message properties are returned, but we do have an

HTTP_ERROR_BODY property.

{"error":{"code":"V1/320","message":{"lang":"en","value":"No item category available (Table T184 OR TEXT )"},"innererror":{"application":{"component_id":"SD-SLS-SO","service_namespace":"/SAP/","service_id":"API_SALES_ORDER_SRV","service_version":"0001"},"transactionid":"64205A9E09A00030E0061F9CEDB7A798","timestamp":"","Error_Resolution":{"SAP_Transaction":"","SAP_Note":"See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"},"longtext_url":"/sap/opu/odata/iwbep/message_text;o=BACKEND/","errordetails":[{"ContentID":"","code":"/IWBEP/CX_MGW_BUSI_EXCEPTION","message":"No item category available (Table T184 OR TEXT )","longtext_url":"/sap/opu/odata/iwbep/message_text;o=BACKEND/","propertyref":"","severity":"error","transition":false,"target":""}]}}}
  1. No properties entered.

Although they are set to Mandatory in the Behaviour Definition, /iwbep/cx_gateway IS NOT CAUGHT.

I've tried to include as much detail as possible but just to clarify, the 2 issues are:

a) I need a consistent way of trapping all exceptions, so I can populate the messages table, which sets up the returned message(s) back to the UI.

b) I need to be able to catch all Mandatory fields that haven't been populated.

Thank you in advance.enter code here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source