Skip to content

Instantly share code, notes, and snippets.

@flaiker

flaiker/exception.abap

Last active Jan 4, 2021
Embed
What would you like to do?
Backport of if_t100_dyn_msg
" Conditions:
" - Longtext support when using MESSAGE lx_ex TYPE 'S' DISPLAY LIKE 'E'
" - Where-used-list support where possible for messages in message classes
" 1. You have a message and want to raise an exception
MESSAGE e053(sv) INTO DATA(lv_dummy) ##NEEDED.
RAISE EXCEPTION TYPE /abc/cx_bc_no_authority
EXPORTING
is_msg = /abc/cl_bc_exc_tools=>get_msg_from_sy( ).
" 2. A function module raises a classic exception with a message
CALL FUNCTION 'CHECK_AUTH_RAISING_MSG'
EXCEPTIONS
fatal_error = 01
OTHERS = 02.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /abc/cx_bc_no_authority
EXPORTING
is_msg = /abc/cl_bc_exc_tools=>get_msg_from_sy( ).
ENDIF.
" 3. Something raises an OO exception but you want to wrap it with a different one
TRY.
OPEN DATASET iv_path
FOR INPUT
IN TEXT MODE
ENCODING DEFAULT
IGNORING CONVERSION ERRORS.
CATCH cx_sy_file_authority INTO DATA(lx_ex).
RAISE EXCEPTION TYPE /abc/cx_bc_no_authority
EXPORTING
is_msg = /abc/cl_bc_exc_tools=>get_msg_from_exc( lx_ex )
ix_previous = lx_ex.
ENDTRY.
" 4. You want to raise an exception and the exception class already has a message available
AUTHORITY-CHECK OBJECT 'S_TCODE'
FOR USER lv_user
ID 'TCD' FIELD iv_tcode.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /abc/cx_bc_no_authority
EXPORTING
is_textid = /abc/cx_bc_no_authority=>gc_tcode
iv_tcode = iv_tcode
iv_user = lv_user.
ENDIF.
" Implementation (bc_dynamic_check and bc_no_check ommitted, they are the same as bc_static_check)
INTERFACE /abc/if_bc_exception PUBLIC.
INTERFACES:
if_t100_message.
CONSTANTS:
BEGIN OF gc_default_message,
msgid TYPE symsgid VALUE '/ABC/BC',
msgno TYPE symsgno VALUE '005',
attr1 TYPE scx_attrname VALUE 'MS_MSG-MSGV1',
attr2 TYPE scx_attrname VALUE '',
attr3 TYPE scx_attrname VALUE '',
attr4 TYPE scx_attrname VALUE '',
END OF gc_default_message.
DATA:
ms_msg TYPE /abc/bc_msg READ-ONLY.
ENDINTERFACE.
CLASS /abc/cx_bc_static_check DEFINITION
PUBLIC
INHERITING FROM cx_static_check
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES:
/abc/if_bc_exception.
ALIASES:
ms_msg FOR /abc/if_bc_exception~ms_msg,
ms_t100_key FOR if_t100_message~t100key,
gc_default_message FOR /abc/if_bc_exception~gc_default_message.
METHODS:
constructor IMPORTING is_textid LIKE if_t100_message=>t100key OPTIONAL
is_msg TYPE /abc/bc_msg OPTIONAL
ix_previous LIKE previous OPTIONAL.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS /abc/cx_bc_static_check IMPLEMENTATION.
METHOD constructor.
super->constructor( previous = ix_previous ).
ms_msg = is_msg.
CLEAR textid.
IF is_msg IS NOT INITIAL.
if_t100_message~t100key = VALUE #(
msgid = is_msg-msgid
msgno = is_msg-msgno
attr1 = '/ABC/IF_BC_EXCEPTION~MS_MSG-MSGV1'
attr2 = '/ABC/IF_BC_EXCEPTION~MS_MSG-MSGV2'
attr3 = '/ABC/IF_BC_EXCEPTION~MS_MSG-MSGV3'
attr4 = '/ABC/IF_BC_EXCEPTION~MS_MSG-MSGV4'
).
ELSEIF is_textid IS INITIAL.
if_t100_message~t100key = /abc/if_bc_exception=>gc_default_message.
ms_msg-msgv1 = CAST cl_abap_objectdescr( cl_abap_typedescr=>describe_by_object_ref( me ) )->get_relative_name( ).
ELSE.
if_t100_message~t100key = is_textid.
ENDIF.
ENDMETHOD.
ENDCLASS.
CLASS /abc/cx_bc_no_authority DEFINITION
PUBLIC
INHERITING FROM /abc/cx_bc_static_check
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
CONSTANTS:
BEGIN OF gc_no_arguments,
msgid TYPE symsgid VALUE '/ABC/BC',
msgno TYPE symsgno VALUE '010',
attr1 TYPE scx_attrname VALUE '',
attr2 TYPE scx_attrname VALUE '',
attr3 TYPE scx_attrname VALUE '',
attr4 TYPE scx_attrname VALUE '',
END OF gc_no_arguments,
BEGIN OF gc_tcode,
msgid TYPE symsgid VALUE '/ABC/BC',
msgno TYPE symsgno VALUE '011',
attr1 TYPE scx_attrname VALUE 'MV_TCODE',
attr2 TYPE scx_attrname VALUE '',
attr3 TYPE scx_attrname VALUE '',
attr4 TYPE scx_attrname VALUE '',
END OF gc_tcode,
BEGIN OF gc_auth_object,
msgid TYPE symsgid VALUE '/ABC/BC',
msgno TYPE symsgno VALUE '012',
attr1 TYPE scx_attrname VALUE 'MV_AUTH_OBJECT',
attr2 TYPE scx_attrname VALUE '',
attr3 TYPE scx_attrname VALUE '',
attr4 TYPE scx_attrname VALUE '',
END OF gc_auth_object.
METHODS:
constructor IMPORTING is_textid LIKE if_t100_message=>t100key OPTIONAL
is_msg TYPE /abc/bc_msg OPTIONAL
iv_tcode TYPE syst_tcode OPTIONAL
iv_user TYPE syst_uname OPTIONAL
iv_auth_object TYPE xuobject OPTIONAL
ix_previous LIKE previous OPTIONAL.
DATA:
mv_tcode TYPE syst_tcode READ-ONLY,
mv_user TYPE syst_uname READ-ONLY,
mv_auth_object TYPE xuobject READ-ONLY.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS /abc/cx_bc_no_authority IMPLEMENTATION.
METHOD constructor.
super->constructor( is_textid = is_textid is_msg = is_msg ix_previous = ix_previous ).
IF ms_t100_key = gc_default_message.
ms_t100_key = gc_no_arguments.
ENDIF.
mv_tcode = iv_tcode.
mv_user = iv_user.
mv_auth_object = iv_auth_object.
ENDMETHOD.
ENDCLASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment