1

I'm using Spring Boot 1.5.3, Spring Data REST, Spring HATEOAS, Hibernate. Spring Data REST manage in a pretty way exceptions, returning a well formatted JSON object like this:

    {
    "timestamp": "2017-06-24T16:08:54.107+0000",
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
    "message": "org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint",
    "path": "/api/v1/workSessions/start"
}

I need to localize exception messages and I'd like to keep the same JSON format of Spring Data REST and take a look how they create the exception object. I'm looking for the code where the exception is created in source code but I am not able to find that. Maybe ExceptionMessage is useful but it has not the structure of the object that at the end arrive to the user.

Where is the point where the exception is created?

2
  • from the exception message it seems that the exception is created during the saving phase. It seems that there are some relation to a non really created object in the DB Commented Jun 25, 2017 at 16:48
  • @AngeloImmediata I don't care about the exception itself. I'm interested in how Spring Data REST create the JSON Object in that way. Independently from the exception, the JSON object is always that. I'd like to find the code where that object is created. Thanks Commented Jun 25, 2017 at 16:51

2 Answers 2

2

Finally I found a useful link before I didn't see: Modify default JSON error response from Spring Boot Rest Controller.

So the object I was looking for is here: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/DefaultErrorAttributes.java

Sign up to request clarification or add additional context in comments.

Comments

1

there is an answer , from useful link

As described in the documentation on error handling, you can provide your own bean that implements ErrorAttributes to take control of the content.

here is example from documentation :

@ControllerAdvice(basePackageClasses = FooController.class)
public class FooControllerAdvice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(YourException.class)
    @ResponseBody
    ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable ex) {
        HttpStatus status = getStatus(request);
        return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }
  }

just inject Locale into handleControllerException method and MessageSource into advice , and in handleControllerException get localize exception messages that you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.