Error handling when calling other system using RFC

Preparation

You have configured outbound connection and started to write code.

Iteration 1

CALL FUNCTION ‘RFC_PING’ DESTINATION ‘DUMMY’.

Looks fine enough. But if you make mistake or connection is temporarily down or happens something else, you will get short dumps CALL_FUNCTION_REMOTE_ERROR,  CALL_FUNCTION_NO_DEST or CALL_FUNCTION_OPEN_ERROR etc.

You should always avoid short dumps in common environment.

 

Iteration 2

When you use CALL FUNCTION … DESTINATION … statement you should always add EXCEPTIONS section.

CALL FUNCTION ‘RFC_PING’ DESTINATION ‘NONE’
  EXCEPTIONS
    communication_failure = 1
    system_failure        = 2.

These system exceptions are part of RFC concept and do not exist in FM implementation.

Looks better.

In most cases dev-team just checks sy-subrc and displays error with common message:

IF sy-subrc NE 0.
  MESSAGE e001(zzz). "Connection error. Try again later
ENDIF.

You should get details. Why? Because it saves team’s time.

When user or support team member faces the indefinite message he will spent more time or/and involve more people to identify the root cause.

Also definite message error is very useful for bug reporting.

Iteration 3

To get text of error message you should add MESSAGE key word.

DATA:  lv_msg TYPE char200.
CLEAR lv_msg.
CALL FUNCTION ‘RFC_PING’ DESTINATION ‘NONE’
  EXCEPTIONS
    communication_failure = 1 MESSAGE lv_msg
    system_failure        = 2 MESSAGE lv_msg.
IF sy-subrc NE 0.
  WRITE: / sy-subrc, lv_msg.
  MESSAGE e001(zzz) with lv_msg. "Connection error: &1
ENDIF.

Is it best solution?

No, if you use message-based error handling and logging, sometimes you will receive cropped message:

Error when opening an RFC connection (CPIC-CALL: ‘

Iteration 4

For an easy solution just add substring manipulation:

MESSAGE i000(zzz) 
  WITH lv_msg(50) 
       lv_msg+50(50) 
       lv_msg+100(50) 
       lv_msg+150(50). "System error: &1&2&3&4

Here is new error message:

Error when opening an RFC connection (CPIC-CALL: ‘ThSAPOCMINIT’, communication rc: CM_RESOURCE_FAILURE_RETRY (cmRc=27).

Some developers find this snippet ugly, but I have no problem with that.

Iteration 5

You can use structures instead:

DATA: BEGIN OF ls_msg,
        msgv1 TYPE char50,
        msgv2 TYPE char50,
        msgv3 TYPE char50,
        msgv4 TYPE char50,
      END OF ls_msg.
CLEAR ls_msg.
CALL FUNCTION ‘RFC_PING’ DESTINATION ‘DUMMY’
  EXCEPTIONS
    communication_failure = 1 MESSAGE ls_msg
    system_failure        = 2 MESSAGE ls_msg.
IF sy-subrc NE 0.
  MESSAGE i000(zzz)
    WITH ls_msg-msgv1
         ls_msg-msgv2
         ls_msg-msgv3
         ls_msg-msgv4. "System error: &1&2&3&4
ENDIF.

Verbose code or not?

 

Thank you for reading.

Добавить комментарий

Ваш адрес email не будет опубликован.