Question
The task is as follows:
There is a simple table for tracking the receipt and withdrawal of materials with a separate column as a cumulative register, where the stock balance after the operation is also calculated. If the balance becomes less than 0 after the operation, you need to:
I manage to do one thing, but not both. Either the error with rollback occurs, or the data is written, and the operation goes through.
How exactly can you use the tryCatch function for this, or is there another way to do it?
Answer
This can be done using tryCatch
For example, we set a field with the number of code-actions when adding:
=catch: if(condition: $#nfv < 0; then: $err)
err: errorException(text: "Zero error")
catch: insert(table: 'error_table'; field: 'error' = str`"Zero value in row" ++ #id`)
If there are several initiating codes that change one field and you need to find out which one caused the tracked event, the initiating code-actions should record information about themselves in a process variable.
For example. This is the code of a button that adds a row with an inappropriate value:
a1=: procVar(name: "zero_process_var"; value: "some_code_name")
a2=: insert(table: 'target_table'; field: 'test' = -1)
Action code in the test field of the target_table:
=catch: if(condition: $#nfv < 0; then: $err)
err: errorException(text: "Zero error")
catch: insert(table: 'error_table'; field: 'error' = str`"Zero value in row" ++ #id ++ $@zero_process_var`)
And in catch, we use data from the variable $@zero_process_var
.