|
@@ -11,39 +11,40 @@ import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
import cn.com.qmth.examcloud.core.print.common.Result;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
-import static cn.com.qmth.examcloud.core.print.common.Result.error;
|
|
|
-
|
|
|
@ControllerAdvice(annotations = Controller.class)
|
|
|
public class AspectController {
|
|
|
private final static Logger log = LoggerFactory.getLogger(AspectController.class);
|
|
|
+ public final static HttpStatus ERROR_CODE = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
|
+ public final static String ERROR_CONTENT = "系统异常!";
|
|
|
|
|
|
@ResponseBody
|
|
|
@ExceptionHandler(value = RuntimeException.class)
|
|
|
- public Result handle(RuntimeException e) {
|
|
|
- if (e instanceof StatusException) {
|
|
|
- StatusException se = (StatusException) e;
|
|
|
- log.error(se.toJson());
|
|
|
- return new Result(se.getCode(), se.getDesc());
|
|
|
- }
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- return error(e.getMessage());
|
|
|
+ public ResponseEntity<String> handle(RuntimeException e) {
|
|
|
+ return doHandle(e);
|
|
|
}
|
|
|
|
|
|
@ResponseBody
|
|
|
@ExceptionHandler(value = Exception.class)
|
|
|
- public Result handle(Exception e) {
|
|
|
+ public ResponseEntity<String> handle(Exception e) {
|
|
|
+ return doHandle(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ResponseEntity<String> doHandle(Exception e) {
|
|
|
if (e instanceof StatusException) {
|
|
|
StatusException se = (StatusException) e;
|
|
|
log.error(se.toJson());
|
|
|
- return new Result(se.getCode(), se.getDesc());
|
|
|
+ return new ResponseEntity<>(se.toJson(), ERROR_CODE);
|
|
|
}
|
|
|
log.error(e.getMessage(), e);
|
|
|
- return error(e.getMessage());
|
|
|
+ String errorJson = Result.error(ERROR_CONTENT).toJson();
|
|
|
+ return new ResponseEntity<>(errorJson, ERROR_CODE);
|
|
|
}
|
|
|
|
|
|
}
|