Groovy Week 2020: Logging and Error Handling
In our second installment of the 2020 Groovy Week, we are going to cover the topic of adding messages to the transformation log as well as showing how to handle situations where an error needs to be raised, e.g. due to incomplete or unexpected source data. Like the previous post, note that this article assumes you have working knowledge of hale»studio and know the terminology.
Tuesday's Script: Handling and logging Errors and other relevant information (Florian)
When using a Groovy script for a specific mapping task, you often need to add checks that verify that the incoming source data can actually be transformed correctly. In cases where the transformation cannot proceed (e.g. because of missing or erroneous source data), the cause needs to be reported. To achieve that, you can add error, warning and informational messages to the transformation log as well as throw exceptions to signal a critical error.
The following script is part of an alignment that transforms road data to the INSPIRE Transport Network Roads schema. It first defines two conditions hatFahrbahnachse
and hatStrassenachse
based on if the source instance contains a link to a AX_Fahrbahnachse
or AX_Strassenachse
source instance. If neither condition is met, the transformation cannot proceed. An error message is added to the transformation log and a NoResultException
is thrown to indicate that the script did not create a result in this case, which is a critical error.
Not all situations are equally critical, of course. If a warning or informational message is sufficient, use one of the two other log levels that are supported by hale studio: warning and info. To emit a warning to the transformation log, use _log.warn
. For informational messages, use _log.info
.
All messages emitted from a Groovy script can be accessed in hale studio via the "Instance transformation" entry of a transformation run in the "Report List", as shown in the following picture:
You can download the script snippet here and import it in hale»studio as a Groovy snippet by going to File -> Import -> Groovy Snippet. Please note that some scripts use protected functions, so you might need to "Lift Groovy Restrictions" to execute the script. Make sure you replace the placeholder attribute names with your own attribute names.
Happy transforming!