Commit 8fa2cc07 by Jason Song

reformat

parent 07bd7c06
...@@ -3,6 +3,9 @@ package com.ctrip.framework.apollo.common.controller; ...@@ -3,6 +3,9 @@ package com.ctrip.framework.apollo.common.controller;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.ctrip.framework.apollo.common.exception.AbstractApolloHttpException;
import com.dianping.cat.Cat;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
...@@ -15,9 +18,6 @@ import org.springframework.web.bind.annotation.ControllerAdvice; ...@@ -15,9 +18,6 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.HttpStatusCodeException;
import com.ctrip.framework.apollo.common.exception.AbstractApolloHttpException;
import com.dianping.cat.Cat;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
...@@ -34,74 +34,75 @@ import static org.springframework.http.MediaType.APPLICATION_JSON; ...@@ -34,74 +34,75 @@ import static org.springframework.http.MediaType.APPLICATION_JSON;
@ControllerAdvice @ControllerAdvice
public class GlobalDefaultExceptionHandler { public class GlobalDefaultExceptionHandler {
private Gson gson = new Gson(); private Gson gson = new Gson();
private static Type mapType = new TypeToken<Map<String, Object>>() {}.getType(); private static Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
private static final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);
private static final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);
//处理系统内置的Exception
@ExceptionHandler(Throwable.class) //处理系统内置的Exception
public ResponseEntity<Map<String, Object>> exception(HttpServletRequest request, Throwable ex) { @ExceptionHandler(Throwable.class)
return handleError(request, INTERNAL_SERVER_ERROR, ex); public ResponseEntity<Map<String, Object>> exception(HttpServletRequest request, Throwable ex) {
} return handleError(request, INTERNAL_SERVER_ERROR, ex);
}
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, @ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
ServletException ex) { public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
return handleError(request, BAD_REQUEST, ex); ServletException ex) {
} return handleError(request, BAD_REQUEST, ex);
}
@ExceptionHandler(HttpStatusCodeException.class)
public ResponseEntity<Map<String, Object>> restTemplateException(HttpServletRequest request, @ExceptionHandler(HttpStatusCodeException.class)
HttpStatusCodeException ex) { public ResponseEntity<Map<String, Object>> restTemplateException(HttpServletRequest request,
return handleError(request, ex.getStatusCode(), ex); HttpStatusCodeException ex) {
} return handleError(request, ex.getStatusCode(), ex);
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<Map<String, Object>> accessDeny(HttpServletRequest request, @ExceptionHandler(AccessDeniedException.class)
AccessDeniedException ex) { public ResponseEntity<Map<String, Object>> accessDeny(HttpServletRequest request,
return handleError(request, FORBIDDEN, ex); AccessDeniedException ex) {
} return handleError(request, FORBIDDEN, ex);
}
//处理自定义Exception
@ExceptionHandler({AbstractApolloHttpException.class}) //处理自定义Exception
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, AbstractApolloHttpException ex) { @ExceptionHandler({AbstractApolloHttpException.class})
return handleError(request, ex); public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, AbstractApolloHttpException ex) {
} return handleError(request, ex);
}
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
AbstractApolloHttpException ex) { private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
return handleError(request, ex.getHttpStatus(), ex); AbstractApolloHttpException ex) {
} return handleError(request, ex.getHttpStatus(), ex);
}
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
HttpStatus status, Throwable ex) { private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
String message = ex.getMessage(); HttpStatus status, Throwable ex) {
String message = ex.getMessage();
logger.error(message, ex);
Cat.logError(ex); logger.error(message, ex);
Cat.logError(ex);
Map<String, Object> errorAttributes = new HashMap<>();
Map<String, Object> errorAttributes = new HashMap<>();
//如果是admin server引起的异常,则显示内部的异常信息
if (ex instanceof HttpStatusCodeException){ //如果是admin server引起的异常,则显示内部的异常信息
errorAttributes = gson.fromJson(((HttpStatusCodeException)ex).getResponseBodyAsString(), mapType); if (ex instanceof HttpStatusCodeException) {
status = ((HttpStatusCodeException)ex).getStatusCode(); errorAttributes = gson.fromJson(((HttpStatusCodeException) ex).getResponseBodyAsString(), mapType);
} else { status = ((HttpStatusCodeException) ex).getStatusCode();
errorAttributes.put("status", status.value()); } else {
errorAttributes.put("message", message); errorAttributes.put("status", status.value());
errorAttributes.put("timestamp", errorAttributes.put("message", message);
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); errorAttributes.put("timestamp",
errorAttributes.put("exception", ex.getClass().getName()); LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
errorAttributes.put("exception", ex.getClass().getName());
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON); HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<>(errorAttributes, headers, status); headers.setContentType(APPLICATION_JSON);
} return new ResponseEntity<>(errorAttributes, headers, status);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment