Skip to content

Commit

Permalink
改进异常处理
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Nov 14, 2015
1 parent 16e96e6 commit b821bdd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void logError(Exception ex, HttpServletRequest request) {
map.put("message", ex.getMessage());
map.put("from", request.getRemoteAddr());
String queryString = request.getQueryString();
map.put("uri", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI());
map.put("path", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI());

logger.error(jsonMapper.toJson(map), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,60 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springside.modules.mapper.JsonMapper;
import org.springside.modules.web.MediaTypes;

import com.google.common.collect.Maps;

/**
* 重载替换Spring Boot默认的BasicErrorController
* 重载替换Spring Boot默认的BasicErrorController, 增加日志并让错误返回方式统一.
*
* @author calvin
*
*/
@Controller
public class ErrorPageController implements ErrorController {

private Logger logger = LoggerFactory.getLogger(ErrorPageController.class);

private JsonMapper jsonMapper = new JsonMapper();
private static Logger logger = LoggerFactory.getLogger(ErrorPageController.class);

@Value("${error.path:/error}")
private String errorPath;

@Override
public String getErrorPath() {
return this.errorPath;
}
private JsonMapper jsonMapper = new JsonMapper();

private ErrorAttributes errorAttributes = new DefaultErrorAttributes();

@RequestMapping(value = "${error.path:/error}", produces = MediaTypes.JSON_UTF_8)
@ResponseBody
public ErrorResult handle(HttpServletRequest request) {
Map<String, Object> attributes = getErrorAttributes(request);

ErrorResult result = new ErrorResult();
result.code = getStatus(request).value();
result.message = (String) request.getAttribute("javax.servlet.error.message");
result.code = (int) attributes.get("status");
result.message = (String) attributes.get("error");

logError(result, request);
logError(attributes, request);

return result;
}

private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode != null) {
try {
return HttpStatus.valueOf(statusCode);
} catch (Exception ex) {
}
}
return HttpStatus.INTERNAL_SERVER_ERROR;
private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes, false);
}

private void logError(ErrorResult result, HttpServletRequest request) {
Map<String, Object> map = Maps.newHashMap();
map.put("code", result.code);
map.put("message", result.message);
map.put("from", request.getRemoteAddr());
String queryString = request.getQueryString();
map.put("uri", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI());
private void logError(Map<String, Object> attributes, HttpServletRequest request) {
attributes.put("from", request.getRemoteAddr());
logger.error(jsonMapper.toJson(attributes));
}

logger.error(jsonMapper.toJson(map));
@Override
public String getErrorPath() {
return this.errorPath;
}
}

0 comments on commit b821bdd

Please sign in to comment.