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 method | ASTATUS variable | ASETERROR Subprog | Error handling | Example of controls |
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:
Parameter | Use | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PRO | Code of the property in which the error occurs. If empty, the error is general and not linked to a property. | ||||||||||||||||||
MES | String value containing the error message (in the language of the user). | ||||||||||||||||||
STA | Status. 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:
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.
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 event | Event Codes | Behavior |
---|---|---|
CRUD operation events | AINSERT,AREAD,AUPDATE,ADELETE | The database update should not have been performed. |
Properties events | INIT,GET,CONTROL,PROPAGATE | The 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 events | AINSERT_CONTROL_BEFORE,AINSERT_CONTROL_AFTER,ADELETE_CONTROL_BEFORE,ADELETE_CONTROL_AFTER,AUPDATE_CONTROL_BEFORE,AUPDATE_CONTROL_AFTER | The control failed. The operation 'Update operation' will not be performed (the control is not fulfilled). |
CRUD update events | AINSERT_BEFORE,AINSERT_AFTER,ADELETE_BEFORE,ADELETE_AFTER,AUPDATE_BEFORE,AUPDATE_AFTER | An error occurred in the transaction. The corresponding rollback operation will be executed. |
CRUD error handling events | AINSERT_ROLLBACK,ADELETE_ROLLBACK,AUPDATE_ROLLBACK | An error occurred during the rollback operation. |
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
The following principles must be followed:
this.ASETERROR
method must be used to return an error. The property code must be filled if the error is linked to a Property. This includes all the code executed in the supervisor events (code starting with A). The development partner must also set up the ASTATUS variable to prompt the supervisor to abort the operation whenever an error occurs.this.AGETMAXERROR(PROPERTY)
where PROPERTY is the Property code if the check for the error must be done on a given Property.Local Integer RETURN_VALUERETURN_VALUE=this.AREAD(KEY)If RETURN_VALUE > [V]CST_AWARNING# An error occurredEnd RETURN_VALUEElse # The program continues...Endif
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