0

I am trying to control how the error is going to be displayed.

I am trying to do something similar to this: https://stackoverflow.com/a/22271986/10538678 but on global level (for every exception thrown). I tried this example described in this question, but i have no idea how to implement this globally

Instead of:

Exception in thread "threadName" Name of Exception : Description
... ...... ..  // Call Stack

For example i want error to display:

<Error>
    <ErrorCode>customErrorCode</ErrorCode>
    <ErorMsg>Description</ErorMsg>
    <ErrorClass>className</ErrorClass>
    <ErrorThread>threadName</ErrorThread>
</Error>

EDIT: I have multiple dependencies which use exception handling and i cannot modify them.

Matej J
  • 615
  • 1
  • 9
  • 33
  • Do you have a `main` method ? If it's the case, the question you linked still works. Just wrap the instructions in `main` with the try catch clause – Arthur Attout Apr 16 '19 at 09:38
  • I have multiple dependencies which throw errors... I would like to catch every error and change output to xml – Matej J Apr 16 '19 at 11:02

2 Answers2

1

What you need is to add ControllerAdvice. In your case it would be something like this:

@ControllerAdvice
public class RestResponseEntityExceptionHandler 
  extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = { Exception.class})
    protected ResponseEntity<String> handleException(Exception e) {
        String bodyOfResponse = "This should be application specific";
        return new ResponseEntiy<String>(bodyOfResponse )
    }
}
0

You can set a DefaultExceptionHandler with Thread.setDefaultUncaughtExceptionHandler(); as the name tells you, it handels uncaught Exception thrown with throw but not printed stack trace!

But if somewhere in youre code you used try{}catch(Exception e){e.printStackTrace();} you cant do enything to change the way it is presented

Starmixcraft
  • 389
  • 1
  • 14
  • Hmm that is interesting.. In code, i have calls like theese: void functionName() throws Exception { ... } – Matej J Apr 16 '19 at 11:18