OData Error Handling
OData Error Handling
Success Responses
Success Codes indicate that the request is successfully processed / received. (2XX)
Response Code Information
200 (Ok) Indicates that the request was successful, and the response
contains the requested data. This is common for GET requests.
201 (Created) Indicates that the request was successful, and a new resource
was created. Commonly used for successful POST requests.
202 (Accepted) Indicates that a batch request has been accepted for processing,
but that the processing has not been completed.
204 (No Content) The request was successful, but there is no content to return in
the response body. Often used for successful DELETE requests or
updates where no response body is needed.
Error Responses
Error Codes indicate that there is an error within the request or in the response. (4XX/5XX)
Response Code Information
400 (Bad Request) Indicates that the payload, request headers, or
request URI provided in a request are not correctly
formatted according to the syntax rules defined in this
document
403 (Not Found / Forbidden) Service not found
404 (Not Found) Resource not found
405 (Method Not Allowed) Incorrect URI & HTTP method combination
501 (Not Implemented) Method is not implemented
500 (Internal Server Error) Indicates that a request being processed by a data
service encountered an unexpected error during
processing.
415 (Unsupported Media Type) The server refused to accept the request because the
message content format is not supported
200 (OK)
201 (Created)
202 (Accepted)
204 (No Content)
As Gateway can be either embedded or deployed as a central hub, there are front-end and
back-end components to SAP Gateway. Similarly, there are front-end and back-end error log
transactions. You can launch the error log with transaction /IWFND/ERROR_LOG in Gateway
Hub systems. Launch the error log with transaction /IWBEP/ERROR_LOG in your back-end
system.
Active Source – The Active Source button enables you to navigate directly to the source code
where the error occurred and the exception rose.
Error Context – For further information, select the line with the error you are interested in.
The error context provides detailed information about the error that occurred
Download to PC – If you are not able to solve the problem by yourself, you can download the
error description by choosing the Download to PC button.
Upload from PC – you can upload the error description to another system in order to check it
there
To reach the Backend Errors, use transaction code /IWBEP/ERROR_LOG in the backend
system or you can reach it from transaction /IWFND/ERROR_LOG by using the
dedicated Backend Monitor button. It will redirect you to the backend system.
Similar buttons can be found here as in the frontend Error Log to get further information about
the error:
• Error Context
• Active Source
• Download to PC / Upload from PC
• Service Implementation
Exception Handling in Odata
One important aspect of developing the OData service is to use the exceptions correctly to
convey errors to front-end consumers. A typical way to do this is to use
exceptions /iwbep/cx_mgw_busi_exception and /iwbep/cx_mgw_tech_exception from the
methods.
This exception is used to signal errors that arise from business logic or business rule violations.
These errors are typically related to the data itself or the business processes being executed.
Examples include:
This exception is used to signal errors that arise from technical issues or system malfunctions.
These errors are typically related to the underlying infrastructure or code execution.
Examples include:
Message Container:
For raising error messages first, we have to prepare a message container. Message Container
is used to add corresponding success or error messages to the response of the OData service.
We use a standard interface /IWBEP/IF_MESSAGE_CONTAINER to add the messages to the
OData response.
To add a message to the message container, we can use any of these methods in the message
container interface apart from these 5 methods other methods also available.
METHOD customerset_create_entity.
" Validations
IF er_entity-cid_no IS INITIAL.
lo_msg->add_message(
iv_msg_type = /iwbep/cl_cos_logger=>error
iv_msg_id = 'ZMSG_SSS'
iv_msg_number = '001' ).
ELSE.
INSERT zodata_customer FROM er_entity.
ENDIF.
ENDMETHOD.
Output:
Handling Multiple Messages in response:
METHOD customerset_create_entity.
" Validations
IF er_entity-cid_no IS INITIAL.
lo_msg->add_message(
iv_msg_type = /iwbep/cl_cos_logger=>error
iv_msg_id = 'ZMSG_SSS'
iv_msg_number = '001' ).
ENDIF.
IF er_entity-c_name IS INITIAL.
lo_msg->add_message(
iv_msg_type = /iwbep/cl_cos_logger=>error
iv_msg_id = 'ZMSG_SSS'
iv_msg_number = '002' ).
ENDIF.
ELSE.
INSERT zodata_customer FROM er_entity.
ENDIF.
ENDMETHOD.
Output:
ADD_ERROR_DETAIL: Here we have to pass error detail work area of type
/IWBEP/IF_MESSAGE_CONTAINER=>TY_S_ERROR_DETAIL. Since it is work area, we cannot
pass error details directly to method because we have more than one validation. Instead we
have to assign details to a local work area and then we have to pass that work area into that
method parameter.
METHOD customerset_create_entity.
" Validations
IF er_entity-cid_no IS INITIAL.
ENDIF.
IF er_entity-c_name IS INITIAL.
ENDIF.
ENDMETHOD.
Output:
ADD_MESSAGE_TEXT_ONLY: In this method no need of passing message class, id, type
instead we directly have to pass message text and type whether it is Error/ Warning/Success.
METHOD customerset_create_entity.
" Validations
IF er_entity-cid_no IS INITIAL.
lo_msg->add_message_text_only(
EXPORTING
iv_msg_type = 'E'
iv_msg_text = 'Enter Customer id' ).
ENDIF.
IF er_entity-c_name IS INITIAL.
lo_msg->add_message_text_only(
EXPORTING
iv_msg_type = 'E'
iv_msg_text = 'Enter Customer Name' ).
ENDIF.
ENDMETHOD.
Output:
ADD_MESSAGE_FROM_BAPI: We have to pass BAPI work area and also target id i.e, message
class name. since we have to pass work area name to the method first we have to assign BAPI
message detail to work area.
" Validations
IF er_entity-cid_no IS INITIAL.
ls_bapi-id = 'ZMSG_SSS'.
ls_bapi-number = '001'.
ls_bapi-type = 'E'.
lo_msg->add_message_from_bapi(
EXPORTING
is_bapi_message = ls_bapi " Return Parameter
iv_message_target = 'ZMSG_SSS' " Target (reference)(e.g.Property ID) of a message
).
ENDIF.
IF er_entity-c_name IS INITIAL.
ls_bapi-id = 'ZMSG_SSS'.
ls_bapi-number = '002'.
ls_bapi-type = 'E'.
lo_msg->add_message_from_bapi(
EXPORTING
is_bapi_message = ls_bapi " Return Parameter
iv_message_target = 'ZMSG_SSS' " Target (reference)(e.g.Property ID) of a message
).
ENDIF.
METHOD customerset_create_entity.
" Validations
IF er_entity-cid_no IS INITIAL.
ENDIF.
IF er_entity-c_name IS INITIAL.
ENDIF.
lo_msg->add_messages_from_bapi(
EXPORTING
it_bapi_messages = lt_bapi ).
ELSE.
INSERT zodata_customer FROM er_entity.
ENDIF.
ENDMETHOD.
Output: