image.png
    image.png

    1. 上面的这些异常信息我们可以加入一些自定义的内容 ```java @Configuration public class CustomerConfig {

      @Bean public CustomerErrorAttribute customerErrorAttribute(){

      1. return new CustomerErrorAttribute(true);

      }

    }

    1. ```java
    2. public class CustomerErrorAttribute extends DefaultErrorAttributes {
    3. /**
    4. * 最大栈元素
    5. */
    6. private final int MAX_STACK_TRACE_ELEMENT = 5;
    7. public CustomerErrorAttribute(boolean includeException) {
    8. super(includeException);
    9. }
    10. @Override
    11. public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
    12. Map<String, Object> result = super.getErrorAttributes(webRequest, includeStackTrace);
    13. Throwable throwable = super.getError(webRequest);
    14. try {
    15. // 增加exception相关的信息,用来在通过feign调用的客户端进行反序列化,实现feign rpc的异常处理
    16. List<StackTraceElement> steList = new LinkedList<>();
    17. for(StackTraceElement stackTraceElement : throwable.getStackTrace()) {
    18. steList.add(stackTraceElement);
    19. if(steList.size() >= MAX_STACK_TRACE_ELEMENT) {
    20. break;
    21. }
    22. }
    23. throwable.setStackTrace(steList.toArray(new StackTraceElement[steList.size()]));
    24. result.put("message","勇士总冠军");
    25. result.put("exception_detail", JSONObject.toJSONString(throwable));
    26. result.put("author","gaoxi");
    27. result.put("phone","18751669318");
    28. } catch (Exception e) {
    29. e.setStackTrace(new StackTraceElement[0]);
    30. result.put("exception_detail", e);
    31. }
    32. return result;
    33. }
    34. }
    1. 自定义异常信息后

    image.png
    image.png