Exceptions handlers
This page is part of the magic.library documentation.
Magic allows you to provide a custom exceptions handler on a per folder level. This overrides the default exception logic with a custom exception handler expected to be named “exceptions.hl” and found within the folder hierarchy where an HTTP invocation is resolved. For instance, if you wish to create your own exception handler for a specific module called “foo”, you can create an exception handler file called “/files/modules/foo/exceptions.hl”, and expect this file to be invoked every time an unhandled exception occurs, anywhere inside of your “foo” folder. This allows you to transform an unhandled exception, such as for instance localising it or customising it in any ways. Your custom exception handler will be invoked with the following arguments.
- [message] - The exception error message
- [path] - The URL that triggered the exception
- [field] - If the exception that was thrown declared a field, this argument will contain the name of the field
- [status] - If the exception that was thrown declared a status code, this argument will contain the status code
- [public] - If the exception that was thrown wants to propagate to the client this will be true
You can return a “transformed” exception from your exception handler, returning the following arguments, that will end up becoming the response object returned to the client.
- [message] - Message to return as JSON to client
- [field] - Field to return as JSON to client
- [status] - Status code to decorate your HTTP response with
The default exception handler can be found in “/files/exceptions.hl”, which will be invoked if no custom exception handler is declared further down in your folder hierarchy.