G
G
geron2016-12-25 18:25:02
Java
geron, 2016-12-25 18:25:02

How is exception handling implemented in corporate projects?

Hello!

I am writing a spring project, in the project I use spring framework, spring security, spring mvc, spring data, hibernate, mysql, java 8, rest technologies. Many questions arise during the development process, one of them concerns the handling of exceptions on large enterprise projects. On past projects, I somehow didn’t quite understand how exception handling is implemented. Yes, and on my own, somehow I can’t figure it out, I don’t understand this exception handling and that’s it). Can you please tell me how exception handling is implemented on large projects? If possible with examples. More detailed questions.
1-how to handle database errors (Mysql) konstrany, length, fell off the connection to the database, etc.?
2nd i use rest, in response to client i send wrapper

Response<T>{
private boolean status;
    private T data;
    private Error error;
} при ошибки в него кладу объект 
Error {
    private ErrorReason reason; //enum
    private List<ErrorEnum> messages;
}

so I found an example with a common handler for the entire project, but the problem is that then I won’t be able to use my Response wrapper. It turns out that I will not be able to use Response if I use a common handler?
3-are there any standard database error codes that must be processed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Lopatin, 2016-12-26
@lorus

In order to handle exceptions, you can:
1. Use the annotation @ExceptionHandler. In order not to prescribe it in each controller and not use inheritance, it can be applied to a method in a class with an annotation @ControllerAdvice
2. Implement your own HandlerExceptionResolverand register it inWebMvcConfigurer

D
Dmitry Zaitsev, 2016-12-26
@dim_s

In Spring, you can define a handler for all exceptions and errors through the @ExceptionHandler annotation, create a base controller with a method and this annotation, such as BaseRestController , and inherit all controllers from it. Inside the method, return a JSON object with error status 500 and error text, and don't forget to log the exception, if necessary (for example, via slf4j).
500 status indicates that an internal error has occurred, there are no http statuses for database errors.
This will be enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question