2

We have a 3rd party that places orders onto our system via a JSON payload into an SAP Gateway Service.

The structure of the Gateway Service holds the structure of the data expected in the JSON.

From the Gateway Service, an ABAP function is called which takes the payload and creates the order.

We have a certain level of error handling on the ABAP side, but if there is ANY issue at all with the JSON format, a generic 500 error is returned directly from SAP Gateway without ever getting into the ABAP.

So the question is: Is there any way of returning what the specific error with the JSON is to the end-user?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Adam Harkus
  • 2,050
  • 2
  • 36
  • 64

1 Answers1

1

First, the main GW log is located in /IWFND/ERROR_LOG tcode. It holds both system and custom Gateway errors, and looks like this

enter image description here

The general approach to do error logging in SAP Gateway is:

  1. Put your custom error message and raise business exception in JSON parameter validation method

It is done while getting input parameters, it can be a GetEntity or CreateEntity method of MPC_EXT class. As we speak about order creation, probably it will be a CreateEntity, and there you can analyze the structure of the JSON string and validate it. Analyzing JSON is out of the scope of this question.

  1. When errors are found the exception will be fired and it will be showed both in log and in browser console

enter image description here

There are two main types of exceptions in Gateway: /iwbep/cx_mgw_busi_exception and /iwbep/cx_mgw_tech_exception, but as we want to inject custom logic, our choice is the former.

General approach to implement the exception-handling is:

IF json_invalid = abap_true.

DATA(lo_message_container) = me->mo_context->get_message_container( ).
lo_message_container->add_message( iv_msg_type = /iwbep/cl_cos_logger=>error
                                   iv_msg_number = '100'
                                   iv_msg_id = 'ZJSO'
                                   iv_add_to_response_header = abap_true
                                 ).

RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
 EXPORTING
  message_container = lo_message_container.

Important: do not miss iv_add_to_response_header = abap_true parameter when adding the message, thus you will be able to read error messages directly in response without going to logs.

As your business requirement consists of creating order from JSON, probably you will need
add_messages_from_bapi method:

lo_message->add_messages_from_bapi( it_bapi_messages = lt_return_msg ).

it eats this exact BAPIRET2 structure that comes from your order creation BAPI.

Finally, after all done it worth tracing payload via /IWFND/TRACE to check what payload comes to front-end.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90