下面列出了org.springframework.http.HttpStatus#is5xxServerError ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@ExceptionHandler(Exception.class)
public void handleException(Exception e, HttpServletResponse response, HttpServletRequest request)
throws IOException {
storeException(request, response, e);
ResponseStatus responseStatus =
AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class);
if (responseStatus != null) {
HttpStatus httpStatus = responseStatus.value();
if (httpStatus.is5xxServerError()) {
logger.error(httpStatus.getReasonPhrase(), e);
} else if (httpStatus != HttpStatus.NOT_FOUND) {
logger.error(httpStatus.getReasonPhrase() + ": " + e.toString());
}
String message = e.getMessage();
if (message == null || message.trim().isEmpty()) {
message = responseStatus.reason();
}
response.sendError(httpStatus.value(), message);
} else {
logger.error("Internal Server Error", e);
response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
}
@ExceptionHandler(ServiceException.class)
@ResponseBody
public ResponseEntity<RestErrorResponse> serviceException(ServiceException ex) {
ServiceError serviceError = ex.getServiceError();
HttpStatus httpStatus = HttpStatus.valueOf(serviceError.getHttpStatus());
RestErrorResponse restErrorResponse = new RestErrorResponse(serviceError, ex.getLocalizedMessage());
String logMessage;
try {
logMessage = ObjectMapperUtils.writeValueAsString(restErrorResponse);
} catch (JsonProcessingException e) {
logMessage = "code : " + ex.getServiceError().getCode() + ", message : " + ex.getLocalizedMessage();
}
if (serviceError.equals(ServiceError.ANONYMOUS)) {
// log.info(logMessage);
} else if (httpStatus.is4xxClientError()) {
log.warn(logMessage, ex);
} else if (httpStatus.is5xxServerError()) {
log.error(logMessage, ex);
}
return new ResponseEntity<>(restErrorResponse, HttpStatus.valueOf(serviceError.getHttpStatus()));
}
@Override
public boolean canRetry(RetryContext context) {
Throwable lastThrowable = context.getLastThrowable();
if (lastThrowable == null) {
return true;
}
if (lastThrowable instanceof ApiException) {
if (context.getRetryCount() <= maxAttempts) {
int code = ((ApiException) lastThrowable).getCode();
if (code != 0) {
HttpStatus httpStatus = HttpStatus.valueOf(code);
boolean httpStatus5xxServerError = httpStatus.is5xxServerError();
LOGGER.warn("{} Exception occurred during CM API call, retryable: {} ({}/{})",
code,
httpStatus5xxServerError,
context.getRetryCount(),
maxAttempts);
return httpStatus5xxServerError;
}
} else {
return false;
}
}
return false;
}
void logForwardingResult(HttpStatus responseStatus, HttpMethod originalRequestMethod, HttpMethod forwardedRequestMethod, URI originalRequestUri, URI forwardedRequestUri, String mappingName) {
String logMessage = "Forwarding: {} {} -> '{}' -> {} {} {}";
if (responseStatus.is5xxServerError()) {
log(serverErrorLogLevel, logMessage, originalRequestMethod, originalRequestUri, mappingName, forwardedRequestMethod, forwardedRequestUri, responseStatus.value());
} else if (responseStatus.is4xxClientError()) {
log(clientErrorLogLevel, logMessage, originalRequestMethod, originalRequestUri, mappingName, forwardedRequestMethod, forwardedRequestUri, responseStatus.value());
} else {
log(successLogLevel, logMessage, originalRequestMethod, originalRequestUri, mappingName, forwardedRequestMethod, forwardedRequestUri, responseStatus.value());
}
}
void logForwardingResult(HttpStatus responseStatus, String mappingName) {
String logMessage = "Asynchronous execution of '{}' request mapping resulted in {} response status";
if (responseStatus.is5xxServerError()) {
log.error(logMessage, mappingName, responseStatus.value());
} else if (responseStatus.is4xxClientError()) {
log.info(logMessage, mappingName, responseStatus.value());
} else {
log.debug(logMessage, mappingName, responseStatus.value());
}
}
public void send( WebMessage webMessage, HttpServletResponse response, HttpServletRequest request )
{
String type = request.getHeader( "Accept" );
type = !StringUtils.isEmpty( type ) ? type : request.getContentType();
type = !StringUtils.isEmpty( type ) ? type : MediaType.APPLICATION_JSON_VALUE;
HttpStatus httpStatus = HttpStatus.valueOf( webMessage.getHttpStatusCode() );
if ( httpStatus.is4xxClientError() || httpStatus.is5xxServerError() )
{
response.setHeader( "Cache-Control", CacheControl.noCache().cachePrivate().getHeaderValue() );
}
// allow type to be overridden by path extension
if ( request.getPathInfo().endsWith( ".json" ) )
{
type = MediaType.APPLICATION_JSON_VALUE;
}
else if ( request.getPathInfo().endsWith( ".xml" ) )
{
type = MediaType.APPLICATION_XML_VALUE;
}
if ( isCompatibleWith( type, MediaType.APPLICATION_JSON ) )
{
sendJson( webMessage, response );
}
else if ( isCompatibleWith( type, MediaType.APPLICATION_XML ) )
{
sendXml( webMessage, response );
}
else
{
sendJson( webMessage, response ); // default to json
}
}
public static void log(
final Throwable throwable,
final HttpStatus status) {
if (status.is4xxClientError()) {
LOG.warn("{}: {}", status.getReasonPhrase(), throwable.getMessage());
} else if (status.is5xxServerError()) {
LOG.error(status.getReasonPhrase(), throwable);
}
}
@ExceptionHandler({ ServiceException.class })
public ModelAndView handleServiceException(HttpServletRequest request, ServiceException exception) {
ServiceError serviceError = exception.getServiceError();
HttpStatus httpStatus = HttpStatus.valueOf(serviceError.getHttpStatus());
RestErrorResponse restErrorResponse = new RestErrorResponse(serviceError, exception.getLocalizedMessage());
String logMessage;
try {
logMessage = ObjectMapperUtils.writeValueAsString(restErrorResponse);
} catch (JsonProcessingException e) {
logMessage = "code : " + exception.getServiceError().getCode() + ", message : " + exception.getLocalizedMessage();
}
ModelAndView modelAndView = new ModelAndView();
if (httpStatus.is4xxClientError()) {
modelAndView.setViewName("error/4xx");
log.warn(logMessage, exception);
} else if (httpStatus.is5xxServerError()) {
modelAndView.setViewName("error/" + httpStatus.toString());
log.error(logMessage, exception);
}
Map<String, Object> map = getErrorAttributes(request, false);
modelAndView.addAllObjects(map);
modelAndView.setStatus(httpStatus);
modelAndView.addObject("status", httpStatus.value());
return modelAndView;
}
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
HttpStatus status = clientHttpResponse.getStatusCode();
return status.is4xxClientError() || status.is5xxServerError();
}