Is it good practice to throw an exception from the DAO level to the controller?

I am writing one REST api. In my DAO layer can be two exceptions, namely Exception_Xand Exception_Y. If I encounter an exception Exception_Xin the DAO layer, my controller should return a status code 200, if Exception_Y, then 401, and if everything goes well, the controller should return 201. Now, that I thought that I would throw an exception that I met, since it is from the DAO level to the controller through the service level and in the controller block catchI will return the answer. Is this acceptable or is there some other standard way?

+4
source share
3 answers

Yes, this is a perfectly acceptable way. However, instead of using it, try-catchI would suggest implementing exception handlers for your REST controllers. This way you do not have to clutter up your REST methods.

In addition, it would be better to create a model object in the REST layer for error messages - ErrorResponsewith relevant information:

class ErrorResponse {
    int statusCode;
    String errorMessage;
}

And return the object from the exception handlers. BTW, you can also map your exception class directly to the response using the annotation @ResponseStatus:

@ResponseStatus(value=401, reason="message")
class Exception_Y extends RuntimeException {
} 

Then you do not need to write an exception handler for this exception.

+5
source

. .

, , , . , PersistentException, ServiceException .., .

spring inbuild ControllerAdvice JsonExceptionModel .

@ControllerAdvice
public class GlobalExceptionHandler {

@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(SQLException.class)
    public Map<String, Object> handleSQLException(HttpServletRequest request, Exception ex) {
        //json response here
    }
}


public class JsonExceptionModel  {
    private int code;
    private String type;
    private String url;
    private String message;
    private String moreInfo;

// getters/setters here
}
+1

I suggest you go with the Exception Resolver that spring provides. The Spring Framework provides the HandlerExceptionResolver interface, which we can implement to create a global exception handler. We can also override it to create our own global handler with changes to our applications, such as logging exception messages.

Here is an example implementation of HandlerExceptionResolver that will fully meet your needs.

public class RestResponseStatusExceptionResolver extends HandlerExceptionResolver {

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                                              Exception ex) {
        if (ex instanceof InvalidInputException) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return handleException(ex);
        } else if (ex instanceof ResourceNotFoundException) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return handleException(ex);
        }


//Adding error details to modelView object
modelAndView.addObject("errors", ErrorDetails);



// Custom error message details
public class ErrorDetails {
    private String code;
    private List<String> data;
}
0
source

All Articles