'How to change Mojolicious Lite default error not found to a custom json response

I'm creating a json web service using Mojolicious Lite.

By default Mojolicious returns a HTML response for a server error or not found error.

Is there a way to overwrite this to a custom JSON response?



Solution 1:[1]

It's been a minute, but in Mojo 9 in a full app I've just been returning JSON and returning the status:

$c->render( json => $json, status => 404 );

But, I also have a catch-all route at the end of my setup:

$self->routes->any('/*')->to( ... );

Solution 2:[2]

The Rendering guide discusses how to customize these responses.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brian d foy
Solution 2 Grinnz