Coverage Report - com.ctrip.framework.apollo.common.controller.GlobalDefaultExceptionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
GlobalDefaultExceptionHandler
0%
0/34
0%
0/8
1.556
GlobalDefaultExceptionHandler$1
0%
0/1
N/A
1.556
 
 1  
 package com.ctrip.framework.apollo.common.controller;
 2  
 
 3  
 import org.slf4j.Logger;
 4  
 import org.slf4j.LoggerFactory;
 5  
 import org.springframework.http.HttpHeaders;
 6  
 import org.springframework.http.HttpStatus;
 7  
 import org.springframework.http.ResponseEntity;
 8  
 import org.springframework.security.access.AccessDeniedException;
 9  
 import org.springframework.web.HttpMediaTypeException;
 10  
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 11  
 import org.springframework.web.bind.annotation.ControllerAdvice;
 12  
 import org.springframework.web.bind.annotation.ExceptionHandler;
 13  
 import org.springframework.web.client.HttpStatusCodeException;
 14  
 
 15  
 import com.ctrip.framework.apollo.core.exception.AbstractBaseException;
 16  
 import com.ctrip.framework.apollo.core.exception.BadRequestException;
 17  
 import com.ctrip.framework.apollo.core.exception.NotFoundException;
 18  
 import com.dianping.cat.Cat;
 19  
 import com.google.gson.Gson;
 20  
 import com.google.gson.reflect.TypeToken;
 21  
 
 22  
 import java.lang.reflect.Type;
 23  
 import java.time.LocalDateTime;
 24  
 import java.time.format.DateTimeFormatter;
 25  
 import java.util.LinkedHashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.servlet.ServletException;
 29  
 import javax.servlet.http.HttpServletRequest;
 30  
 
 31  
 import static org.springframework.http.HttpStatus.BAD_REQUEST;
 32  
 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
 33  
 import static org.springframework.http.HttpStatus.NOT_FOUND;
 34  
 import static org.springframework.http.HttpStatus.UNAUTHORIZED;
 35  
 import static org.springframework.http.MediaType.APPLICATION_JSON;
 36  
 
 37  
 @ControllerAdvice
 38  0
 public class GlobalDefaultExceptionHandler {
 39  0
   private static final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);
 40  
 
 41  0
   private Gson gson = new Gson();
 42  
 
 43  0
   private static Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
 44  
 
 45  
   @ExceptionHandler(Exception.class)
 46  
   public ResponseEntity<Map<String, Object>> exception(HttpServletRequest request, Exception ex) {
 47  0
     logger.error("internal server error", ex);
 48  0
     return handleError(request, INTERNAL_SERVER_ERROR, ex);
 49  
   }
 50  
 
 51  
   @ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
 52  
   public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
 53  
       ServletException ex) {
 54  0
     return handleError(request, BAD_REQUEST, ex);
 55  
   }
 56  
 
 57  
   @ExceptionHandler({BadRequestException.class})
 58  
   public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, BadRequestException ex) {
 59  0
     return handleError(request, BAD_REQUEST, ex);
 60  
   }
 61  
 
 62  
   @ExceptionHandler(NotFoundException.class)
 63  
   public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request,
 64  
       NotFoundException ex) {
 65  0
     return handleError(request, NOT_FOUND, ex);
 66  
   }
 67  
 
 68  
   @ExceptionHandler(HttpStatusCodeException.class)
 69  
   public ResponseEntity<Map<String, Object>> restTemplateException(HttpServletRequest request,
 70  
       HttpStatusCodeException ex) {
 71  0
     logger.error("rest template exception", ex);
 72  0
     Map<String, Object> errorAttributes = gson.fromJson(ex.getResponseBodyAsString(), mapType);
 73  0
     HttpHeaders headers = new HttpHeaders();
 74  0
     headers.setContentType(APPLICATION_JSON);
 75  0
     return new ResponseEntity<>(errorAttributes, headers, ex.getStatusCode());
 76  
   }
 77  
 
 78  
   @ExceptionHandler(AccessDeniedException.class)
 79  
   public ResponseEntity<Map<String, Object>> accessDeny(HttpServletRequest request,
 80  
                                                         AccessDeniedException ex) {
 81  0
     return handleError(request, UNAUTHORIZED, ex);
 82  
   }
 83  
 
 84  
   private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
 85  
                                                           HttpStatus status, Throwable ex) {
 86  0
     return handleError(request, status, ex, ex.getMessage());
 87  
   }
 88  
 
 89  
   private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
 90  
                                                           HttpStatus status, Throwable ex, String message) {
 91  0
     ex = resolveError(ex);
 92  0
     if (ex.getCause() instanceof HttpStatusCodeException) {
 93  0
       return restTemplateException(request, (HttpStatusCodeException) ex.getCause());
 94  
     }
 95  
 
 96  0
     Cat.logError(ex);
 97  
 
 98  0
     Map<String, Object> errorAttributes = new LinkedHashMap<>();
 99  0
     errorAttributes.put("status", status.value());
 100  0
     errorAttributes.put("message", message);
 101  0
     errorAttributes.put("timestamp",
 102  0
                         LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
 103  0
     errorAttributes.put("exception", resolveError(ex).getClass().getName());
 104  
 //    errorAttributes.put("stackTrace", ex.getStackTrace());
 105  0
     if (ex instanceof AbstractBaseException) {
 106  0
       errorAttributes.put("errorCode", ((AbstractBaseException) ex).getErrorCode());
 107  
     }
 108  0
     HttpHeaders headers = new HttpHeaders();
 109  0
     headers.setContentType(APPLICATION_JSON);
 110  0
     return new ResponseEntity<>(errorAttributes, headers, status);
 111  
   }
 112  
 
 113  
   private Throwable resolveError(Throwable ex) {
 114  0
     while (ex instanceof ServletException && ex.getCause() != null) {
 115  0
       ex = ((ServletException) ex).getCause();
 116  
     }
 117  0
     return ex;
 118  
   }
 119  
 }