Implementation of a more flexible custom error handler