简体   繁体   English

捕获所有异常并返回带有消息列表的异常

[英]Catching all exceptions and returning an exception with list of messages

I'm wondering if there is any elegant way to catch all exceptions (specifically custom runtime exceptions) and return an exception containing a list of the messages. 我想知道是否有任何优雅的方法来捕获所有异常(特别是自定义运行时异常)并返回包含消息列表的异常。

Instead of having a String message, the big exception would then contain String[] message for example. 例如,大异常将不包含String消息,而是包含String []消息。

Scenario: 场景:

A REST request is made to the back-end with a JSON object containing a bunch of fields. 使用包含一堆字段的JSON对象向后端发出REST请求。 I want to validate these fields on the backend and return a list of errors if any exceptions occur. 我想在后端验证这些字段,并在发生任何异常时返回错误列表。

If both the name and lastname field are not acceptable input, I don't want to throw an exception on the invalid name and have the user change the name and submit again only to get an error message that the lastname is invalid too. 如果name和lastname字段都不是可接受的输入,则我不想在无效名称上引发异常,并让用户更改名称并再次提交,只是得到一条错误消息,指出lastname也无效。

Hence why I want to collect all invalid input and return a list of these in the form of an exception. 因此,为什么我要收集所有无效输入并以异常形式返回这些列表。

Spring collects JSR-303/JSR-349 bean validation failures into a BindException : Spring将JSR-303 / JSR-349 bean验证失败收集到BindException

Thrown when binding errors are considered fatal. 当绑定错误被认为是致命的时抛出。 Implements the BindingResult interface (and its super-interface Errors ) to allow for the direct analysis of binding errors. 实现BindingResult接口(及其超级接口Errors )以允许直接分析绑定错误。

Instead of developing your own mechanism for bean validation you might want to read 3. Validation, Data Binding, and Type Conversion and follow the standards. 您可能需要阅读3.验证,数据绑定和类型转换,并遵循标准,而不是开发自己的bean验证机制。

With Spring Boot, you can use the following annotation to handle any kind of Exception for a class or a method : 使用Spring Boot,您可以使用以下注释来处理类或方法的任何类型的Exception:

@ExceptionHandler(YourExceptionHandler.class)

And you can create a class that let you regroup all your custom exception management like this (if you want to gather it) : 然后,您可以创建一个类,让您重新分组所有自定义异常管理,如下所示(如果您想收集它):

@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ResponseStatus(HttpStatus.CONFLICT)  // 409
    @ExceptionHandler(DataIntegrityViolationException.class)
    public void handleConflict() {
        // Nothing to do
    }
}

You can also implement the interface HandlerExceptionResolver to manage all Exceptions that ARE NOT handled by the Controllers (all the others runtime Exceptions) 您还可以实现接口HandlerExceptionResolver来管理控制器未处理的所有异常(所有其他运行时异常)

public interface HandlerExceptionResolver {
    ModelAndView resolveException(HttpServletRequest request, 
            HttpServletResponse response, Object handler, Exception ex);
}

Everything is explained in details here : https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc 此处详细介绍了所有内容: https : //spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

EDIT : I just read that you added up scenario. 编辑 :我刚刚读到您添加了方案。 Actually, for your special case, you should just parse the object, and return one exception (like bad object format, along with a 400 HTTP status code error, with a custom message containing all the fields that are invalid. I guess. 实际上,对于特殊情况,您应该仅解析对象,然后返回一个异常(例如错误的对象格式,以及400 HTTP状态代码错误,以及包含所有无效字段的自定义消息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM