Error handling
When writing PHP functions it is essential to catch possible runtime errors (exceptions) and to treat them explicitly. Use a try-catch block for this.
All exceptions that are not explicitly treated inside the PHP function will be caught by JobRouter and will cause the regular program execution to abort. In case of PHP functions that are executed when a step is send (rule condition and rule execution functions), this leads to the sending being cancelled and the step entering an error state.
Example:
try {
$jobDB = $this->getJobDB();
$sql = 'DELETE FROM JRUSERS WHERE username = ' . $jobDB->quote('jsmith');
$result = $jobDB->exec($sql);
if ($result === false) {
throw new JobRouterException($jobDB->getErrorMessage());
}
} catch (JobRouterException $e) {
// Code for error handling...
}
Error output
You can use exceptions in PHP functions with the intent of creating error messages. In case of PHP functions that are executed when a step is send (rule condition and rule execution functions), this leads to the sending being cancelled and the step entering an error state, with the error message being saved in the step. Process owners can then see the error message in the step overview. In user steps, the error message will be displayed in the form.
To create an error message add the following line to your script:
try {
// Program code causing the JobRouterException
} catch (JobRouterException $e) {
$errorMessage = 'Failed to delete user: ' . $e->getMessage();
throw new JobRouterException($errorMessage);
}
Pass the error message as parameter to the constructor of the JobRouterException.
Example 1 (Output of a validation error):
if ($this->getTableValue('INVOICENUMBER') == '') {
throw new JobRouterException('Missing invoice number.');
}
Example 2 (Output of a database error message):
$jobDB = $this->getJobDB();
$sql = 'DELETE FROM JRUSERS WHERE username = ' . $jobDB->quote('jsmith');
$result = $jobDB->exec($sql);
if ($result === false) {
throw new JobRouterException($jobDB->getErrorMessage());
}