SHARE
TWEET

Untitled

a guest Jun 16th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ControllerAdvice
  2.     public class ExceptionTranslator implements ProblemHandling {
  3.          @ExceptionHandler
  4.         public ResponseEntity<Problem> handleCustomClient1Exception(CustomException1 ex, NativeWebRequest request) {
  5.             Problem problem = Problem.builder()
  6.                     .title(ex.getTitle())
  7.                     .detail(ex.getMessage())
  8.                     .status(ex.getStatusType())
  9.                     .code(ex.getCode())
  10.                     .build();
  11.             return create(ex, problem, request);
  12.         }
  13.          @ExceptionHandler
  14.         public ResponseEntity<Problem> handleCustomClient2Exception(CustomException2 ex, NativeWebRequest request) {
  15.                 Problem problem = Problem.builder()
  16.                         .title(ex.getTitle())
  17.                         .detail(ex.getMessage())
  18.                         .status(ex.getStatusType())
  19.                         .code(ex.getCode())
  20.                         .build();
  21.                 return create(ex, problem, request);
  22.             }
  23.         }
  24.      
  25. public class ClientErrorDecoder implements ErrorDecoder {
  26.     final ObjectMapper mapper;
  27.  
  28.  
  29.     public ClientErrorDecoder() {
  30.         this.mapper = new ObjectMapper();
  31.     }
  32.  
  33. @Override
  34. public Exception decode(String methodKey, Response response) {
  35.     ExceptionDTO exceptionDTO;
  36.  
  37.     try {
  38.         exceptionDTO = mapper.readValue(response.body().asInputStream(), ExceptionDTO.class);
  39.     } catch (IOException e) {
  40.         throw new RuntimeException("Failed to process response body.", e);
  41.     }
  42.  
  43.  
  44.     return new CustomException1(exceptionDTO.getDetail(), exceptionDTO.getCode(), exceptionDTO.getTitle(), exceptionDTO.getStatus());
  45.  
  46.  
  47. }
  48.      
  49. feign:
  50. client:
  51.     config:
  52.         client1:
  53.             errorDecoder: feign.codec.ErrorDecoder.Default
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top