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