Exception stack trace field undefined

**Enonic version:**7.14.4
OS: MacOS

Im trying to access the “exception” field on the error param in my error controller:

export const handleError = (error) => {
  log.info('STACK ' + JSON.stringify(error.exception, null, 2));
  return {
    body: 'Internal server error'
  };
};

But it dosent exist? The code above logs “STACK undefined”. The docs states that the actual Java stack trace should be available: HTTP Error handler - Enonic Developer Portal

NOTE: The code above is written in TypeScript and is compiled to:

var handleError = function(error) {
    log.info("STACK " + JSON.stringify(error.exception, null, 2));
    return {
        body: "Internal server error"
    };
};

I think there is a chance that the real type of error.exception might either be undefined or a Java-object (Which JSON.stringify can’t handle).

Maybe you can try something like this:

type Throwables = { 
  getStackTraceAsString(ex: unknown): string;
};

const Throwables: Throwables = Java.type('com.google.common.base.Throwables');

if (err.exception) {
  log.info(Throwables.getStackTraceAsString(err.exception));
}

– Tom Arild

2 Likes