Error handling

When code is written to be executed through an event, errors and warnings may need to be handled, for example, an erroneous value entered in a field.

This document describes how to trigger an error or a warning message to send to the user.

ASETERROR methodASTATUS variableASETERROR SubprogError handlingExample of controls

1. The ASETERROR method

This is a standard method available on the classes. When an event is called as a method of the class, this is available, and the call fmet this.ASETERROR(PRO,MES,STA) will allow the generation of errors. This method can be invoked several times when more than one error occurs.

The parameters of this method are:


ParameterUse
PROCode of the property in which the error occurs. If empty, the error is general and not linked to a property.
MESString value containing the error message (in the language of the user).
STAStatus. The available statuses are defined by a global variable that contains the corresponding constant value. It is strongly recommended to use these variables rather than a constant value. Available values are:
ConstantValueMeaning
[V]CST_ASUCCESS1The requested operation has been successfully performed.
[V]CST_AINFO2Information message: no error occurs.
[V]CST_AWARNING3Warning message: an unusual set of values has been entered. The system sends a warning and allows you to modify the data entered.
[V]CST_AERROR4An error occurred. The data entered must be modified to execute the operation requested.
[V]CST_AFATAL5A fatal error occurred. A restart of the complete process is necessary.

Note: [V]CST_AOK must not be used with ASETERROR (it returns an error without a message).

When errors or warnings are triggered while using this method, the result is the following:

The supervisor layer considers that an error occurred during a class operation or method if at least a message with a STA value of [V]CST_AERROR or [V]CST_AFATAL has been triggered.

The warning or error messages updated by this method will be displayed if a user's interface is involved. For example, if an error occurs during data entry, the error is displayed on the top of the screen with a link that brings the cursor to the Property field where the error was found.

If no user interface is involved, the development partner calling a method that returns errors can manage them in their own code or write them in a log file.

2. The ASTATUS variable

This variable is available on all the events, whether or notthis is available. The value range is the same as the status values used by the ASETERROR method.

Setting this variable to [V]CST_AERROR or [V]CST_AFATAL is important when an error occurs because these values are tested by the supervisor layer on a return from an event code.

If an error is reported, the supervisor behaves as follows:

Type of eventEvent CodesBehavior
CRUD operation eventsAINSERT,AREAD,AUPDATE,ADELETEThe database update should not have been performed.
Properties eventsINIT,GET,CONTROL,PROPAGATEThe event failed. If this happens during an input, the error will be displayed. If it happens on control at the final stage before the database updates, the updates will not be triggered, but the controls will continue on every Property. A complete list of the errors will be generated.
CRUD control eventsAINSERT_CONTROL_BEFORE,AINSERT_CONTROL_AFTER,ADELETE_CONTROL_BEFORE,ADELETE_CONTROL_AFTER,AUPDATE_CONTROL_BEFORE,AUPDATE_CONTROL_AFTERThe control failed. The operation 'Update operation' will not be performed (the control is not fulfilled).
CRUD update eventsAINSERT_BEFORE,AINSERT_AFTER,ADELETE_BEFORE,ADELETE_AFTER,AUPDATE_BEFORE,AUPDATE_AFTERAn error occurred in the transaction. The corresponding rollback operation will be executed.
CRUD error handling eventsAINSERT_ROLLBACK,ADELETE_ROLLBACK,AUPDATE_ROLLBACKAn error occurred during the rollback operation.

3. The ASETERROR From ASYRSUB subprogram

This subprogram is usually used only by the supervisor layers. It can also be used in some interface programs when the user wants to set up a dedicated HTTP error code. It works exactly as the ASETERROR method, and allows a value to be provided to the HTTP status. This subprogram has the following arguments:
Subprog ASETERROR(ERR,GRP,PRO,MES,NUM,STA)Variable Instance ERR(1..) Using C_AERROR : # Instance of the error array of an instanceValue Char GRP : # Allocation group for the error arrayValue Char PRO : # Property code where the error occursValue Char MES : # Error message in the right languageValue Integer NUM : # http status that has to be sent back in the headerVariable Integer STA : # status (same values as for the ASETERROR method

4. How to handle the errors when calling a method

The following principles must be followed:

An example of a call of a supervisor method is provided below:
Local Integer RETURN_VALUERETURN_VALUE=this.AREAD(KEY)If RETURN_VALUE > [V]CST_AWARNING# An error occurredEnd RETURN_VALUEElse # The program continues...Endif

5. Example of controls

This code is called in a control event on a property that cannot be increased (a warning is sent if the amount is decreased by more than 10%).

# Note : no need to assign [L]ASTATUS=[V]CST_AOK if no error (done by default)Local Decimal DECR_PERCENTIf this.AMOUNT>this.snapshot.AMOUNT[L]ASTATUS=fmet this.ASETERROR("AMOUNT","The amount cannot be greater than"-num$(this.snapshot.AMOUNT),[V]CST_AERROR)ElseDECR_PERCENT=100*(this.snapshot.AMOUNT-this.AMOUNT)/(this.snapshot.AMOUNT+(this.snapshot.AMOUNT=0))If DECR_PERCENT>=10[L]ASTATUS=fmet this.ASETERROR("AMOUNT","Amount has been decreased by"-num$(int(DECR_PERCENT))-"percent",[V]CST_AWARNING)EndifEndifReturn