|
@@ -18,19 +18,23 @@ import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
|
|
|
import cn.com.qmth.stmms.api.exception.ApiException;
|
|
|
import cn.com.qmth.stmms.biz.exam.bean.ResultMessage;
|
|
|
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
|
|
|
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
|
|
|
+import cn.com.qmth.stmms.biz.exception.StatusException;
|
|
|
import cn.com.qmth.stmms.biz.school.model.School;
|
|
|
import cn.com.qmth.stmms.biz.school.service.SchoolService;
|
|
|
+import cn.com.qmth.stmms.common.StatusResponse;
|
|
|
import cn.com.qmth.stmms.common.controller.BaseController;
|
|
|
import cn.com.qmth.stmms.common.domain.ApiUser;
|
|
|
-import cn.com.qmth.stmms.common.domain.WebUser;
|
|
|
import cn.com.qmth.stmms.common.utils.RequestUtils;
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
@@ -38,28 +42,46 @@ public class BaseApiController extends BaseController {
|
|
|
|
|
|
protected static final Logger log = LoggerFactory.getLogger(BaseApiController.class);
|
|
|
|
|
|
- private static final String ERROR_MESSAGE_HEADER_KEY = "error-info";
|
|
|
-
|
|
|
@Autowired
|
|
|
private SchoolService schoolService;
|
|
|
|
|
|
@Autowired
|
|
|
private ExamSubjectService subjectService;
|
|
|
|
|
|
- @ExceptionHandler
|
|
|
- public void exception(HttpServletResponse response, Exception ex) {
|
|
|
- log.error("api execute error", ex);
|
|
|
- response.addHeader(ERROR_MESSAGE_HEADER_KEY, StringUtils.trimToEmpty(ex.getMessage()));
|
|
|
- try {
|
|
|
- if (ex instanceof ApiException) {
|
|
|
- ApiException e = (ApiException) ex;
|
|
|
- response.sendError(e.getCode());
|
|
|
- } else {
|
|
|
- response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("api response senderror", e);
|
|
|
+ @ResponseBody
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ public ResponseEntity<StatusResponse> handleException(Exception e, HttpServletRequest request) {
|
|
|
+ StatusResponse body = new StatusResponse("500", "系统异常");
|
|
|
+ return asResult(e, body, request);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @ExceptionHandler(RuntimeException.class)
|
|
|
+ public ResponseEntity<StatusResponse> handleException(RuntimeException e, HttpServletRequest request) {
|
|
|
+ StatusResponse body = null;
|
|
|
+
|
|
|
+ if (e instanceof StatusException) {
|
|
|
+ StatusException se = (StatusException) e;
|
|
|
+ body = new StatusResponse(se.getCode(), se.getDesc());
|
|
|
+ } else if (e instanceof com.qmth.boot.core.exception.StatusException) {
|
|
|
+ com.qmth.boot.core.exception.StatusException se = (com.qmth.boot.core.exception.StatusException) e;
|
|
|
+ body = new StatusResponse(se.getCode() + "", se.getMessage());
|
|
|
+ } else {
|
|
|
+ body = new StatusResponse("500", "系统异常");
|
|
|
}
|
|
|
+
|
|
|
+ return asResult(e, body, request);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ResponseEntity<StatusResponse> asResult(Throwable err, StatusResponse body, HttpServletRequest request) {
|
|
|
+
|
|
|
+ HttpStatus httpStatus = HttpStatus.SERVICE_UNAVAILABLE;
|
|
|
+
|
|
|
+ log.error(err.getMessage(), err);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Content-Type", "application/json;charset=utf-8");
|
|
|
+ return new ResponseEntity<>(body, headers, httpStatus);
|
|
|
}
|
|
|
|
|
|
protected void exportFile(String fileName, File file, HttpServletResponse response) {
|