exceptionloggingplayframework-2.1

How to get the exception ID and Title from Play!Framework?


I'm using Play!Framework with Java and I'd like to display in the console the exception ID and the title of the exception, and only that.

For testing, I created a Global object like this :

public class Global extends GlobalSettings {
    @Override
    public Result onError(RequestHeader request, Throwable throwable) {
        Logger.info(Json.toJson(throwable).toString());
        return super.onError(request, throwable);
    }
}

This outputs a JSON formatted value of Throwable, wich contains this :

{
    "cause": { ... },
    "stackTrace": [ ... ],
    "title": "Execution exception",
    "description": "[NullPointerException: null]",
    "id": "6epj67c35",
    "message": "Execution exception[[NullPointerException: null]]",
    "localizedMessage": "Execution exception[[NullPointerException: null]]",
    "suppressed": []
}

Which proves that id and title are accessible, but if I try:

throwable.getId(); // does not exists
throwable.getTitle(); // Does not neither

So then, how can I access id and title ?


Solution

  • For that one, I looked at the Play20 code at Github, more precisely at those two classes :

    And in fact, Play throws a PlayException that extends UsefullException, containing the id, title and description. So here's how I did it :

    public class Global extends GlobalSettings {
        @Override
        public Result onError(RequestHeader request, Throwable throwable) {
            if (throwable instanceof PlayException) {
                Logger.info(((PlayException) throwable).id);
            }
            return super.onError(request, throwable);
        }
    }
    

    That's it ! :)