1
votes

I have an ABAP line like this:

assert ( cond = 'x' ).

The caller of this RFC function gets a meaningless message that some assertion failed and I would like to supply an additional explanatory text. This way I can easily find the matching line if the customer send me the error message.

How to do this the most simple way in ABAP?

Update: This question focused on assert, but this is the wrong track in ABAP, I wrote a new and better question here: Raise Exception with custom message in ABAP

3
With RFC, you don't have lots of choices available: either a classic exception or a parameter containing the text. - Sandra Rossi
@SandraRossi how does the most simple way to raise an exception look like? (remeber nobody is ever going to catch the exception) - guettli
@SandraRossi I wrote a new question (including my background context) here: stackoverflow.com/questions/52661797/… - guettli

3 Answers

1
votes

I found that this works:

message my_string_var type 'E'.

It is not an assertion, but it does what I want: It terminates the function and displays my variable.

You can use this handy method to serialize variables, to see their internals. Here variable foo_var gets serialized to json:

MESSAGE |Error foo_var: | && 
     /ui2/cl_json=>serialize( data = foo_var 
     pretty_name = /ui2/cl_json=>pretty_mode-low_case ) TYPE 'E'.
1
votes

From the SAP Documentation:

All methods have the optional import parameters MSG, LEVEL, and QUIT with the same meaning:

  • MSG (type: CSEQUENCE) contains (if available) a text that describes the error in more detail

So you can easily add texts to your assertions like this:

TRY.
 cut->divide_by_zero( denominator = 1).
CATCH cx_sy_zerodivide.
ENDTRY.

cl_abap_unit_assert=>fail( msg = 'CX_SY_ZERODIVIDE not raised'
                           level = if_aunit_constants=>critical ).
0
votes

assert function designed for using in test classes, details are here. I think it is not suitable using in normal codes. You can use check for same functionality but it is not return any message. Raising exceptions in function or class is proper way.

You can return OK and MESSAGE variable for if you don't want to use exceptions. In start of your method/function set OK as empty and MESSAGE such as "there is an error" message. Update message manually before doing something. At the end clear message and set X to MESSAGE.

function ZMKY_TEST.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(COND) TYPE  C
*"  EXPORTING
*"     REFERENCE(OK) TYPE  C
*"     REFERENCE(MESSAGE) TYPE  STRING
*"----------------------------------------------------------------------

  clear: OK, MESSAGE.
  MESSAGE = 'There is an error.'

  MESSAGE = 'COND not equal to X'.
  check COND = 'X'.

  clear: MESSAGE.
  OK = 'X'.

endfunction.