How to log controller errors in browser console

Enonic version: Enonic XP 6.14.1
OS: Windows 10

Hi,

How could I send controller errors to the browser console?

Regards.


Hi Jorge,

So, the controllers in XP are purely backend. You can use log.info to dump stuff into the XP terminal logs.

1 Like

If you have not implemented error handlers in site/error/error.js you will typically get a red error page in the browser with the controller error and the stacktrace. If you want to make your own error messages you can use

throw new Error('My error message');

You can even take it a step further and return your own error response:

try {
    //some code that could fail
} catch (e) {
    return {
        body: '<html><body><p>Your error message</p></body></html>',
        status: 500
    };
}
return {
    body: '<html><body><h1>Normal response</h1></body></html>',
    status: 200
}
1 Like

But typically you don’t want the end user to see server-side error messages. So returning a general error message via site/error/error.js is the way to go.

http://xp.readthedocs.io/en/stable/developer/site/errors/index.html

In your error handler you could do something like:

if(request.mode === 'live') {
    // return generic error page
}

Then the red error page would be let through when mode is ‘edit’ or ‘preview’

1 Like

Sidenote: Error handling is part of the Dev 201 training.

2 Likes