[ABAP] OData: Exceptions / Messages vom Backend an das Frontend hochreichen

Exception-Klassen

* Business Logic Errors: /IWBEP/CX_MGW_BUSI_EXCEPTION
* Technical Errors:      /IWBEP/CX_MGW_TECH_EXCEPTION

Beispiel 1 (Message Container 1)

METHOD xyz_get_entity.
  SELECT SINGLE * FROM abc ...
  IF sy-subrc NE 0.
    mo_context->get_message_container( )->add_message_text_only( iv_msg_type = 'E'
                                                                 iv_msg_text = |Datensatz nicht gefunden.| ).

    RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
      EXPORTING
        message_container = mo_context->get_message_container( ).
  ENDIF.
ENDMETHOD.

Beispiel 2 (Message Container 2)

METHOD xyz_get_entity.
  SELECT SINGLE * FROM abc ...
  IF sy-subrc <> 0.
    mo_context->get_message_container( )->add_message( iv_msg_type   = 'E'
                                                       iv_msg_id     = 'SY'
                                                       iv_msg_number = '500'
                                                       iv_msg_text   = |Datensatz nicht gefunden.| ).

    RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
      EXPORTING
        message_container = mo_context->get_message_container( ).
  ENDIF.
ENDMETHOD.

Beispiel 3 (Message 1)

METHOD xyz_get_entity.
  SELECT SINGLE * FROM abc ...
  IF sy-subrc NE 0.
    RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
      EXPORTING
        textid  = /iwbep/cx_mgw_busi_exception=>business_error
        message = |Datensatz nicht gefunden.|.
  ENDIF.
ENDMETHOD.

Beispiel 4 (Message 2)

METHOD xyz_get_entity.
  SELECT SINGLE * FROM abc ...
  IF sy-subrc NE 0.
    DATA(lv_message) = VALUE scx_t100key( msgid = 'SY'
                                          msgno = '002'
                                          attr1 = |Datensatz nicht gefunden.| ).

    RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
      EXPORTING
        textid  = lv_message.
  ENDIF.
ENDMETHOD.

Beispiel 5 (BAPI return)

METHOD xyz_get_entity.
  DATA: lt_return TYPE bapirettab.
  
  CALL FUNCTION 'BAPI_XYZ'
    ...
    TABLES
      return = lt_return.
  
  IF lt_return IS NOT INITIAL.
    mo_context->get_message_container( )->add_messages_from_bapi( it_bapi_messages = lt_return
                                                                  iv_determine_leading_msg = /iwbep/if_message_container=>gcs_leading_msg_search_option-first ).

    RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
      EXPORTING
        message_container = mo_context->get_message_container( ).
  ENDIF.
ENDMETHOD.

Beispiel 6 (Exception)

METHOD xyz_get_entity.
  TRY.
      ...
    CATCH cx_root INTO DATA(e_txt).
      RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
        EXPORTING
          textid  = /iwbep/cx_mgw_busi_exception=>business_error
          message = |{ e_txt->get_text( ) }|.
  ENDTRY.
ENDMETHOD.