Error handler
When encountering errors from an application’s backend, gathering additional error information, such as error messages, tracing data, or request and response bodies, is often necessary. The initial suggestion is to leverage your observability solution to find these errors. However, capturing error details directly in k6 can also be useful for troubleshooting.
In k6, there are two common approaches to store additional error information:
- Console logs and output k6 logs to Loki, a file, or use Grafana Cloud k6.
- Using Tags in a custom counter metric to track error data.
When deciding what error data to store, consider whether it’s a general or specific error, and be aware that high-load tests may generate a substantial volume of error data.
Below is an example using an ErrorHandler
class to log error information. It accepts a callback that instructs how to log errors. The example provides instructions for both previously mentioned options: console logs and custom metrics.