Java Code Examples for org.springframework.web.server.ResponseStatusException#getStatus()

The following examples show how to use org.springframework.web.server.ResponseStatusException#getStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonExceptionHandler.java    From MyShopPlus with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
    // 按照异常类型进行处理
    HttpStatus httpStatus;
    String body;
    if (throwable instanceof NotFoundException) {
        httpStatus = HttpStatus.NOT_FOUND;
        body = "Service Not Found";
    } else if (throwable instanceof ResponseStatusException) {
        ResponseStatusException responseStatusException = (ResponseStatusException) throwable;
        httpStatus = responseStatusException.getStatus();
        body = responseStatusException.getMessage();
    } else {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        body = "Internal Server Error";
    }

    // 封装响应结果
    Map<String, Object> result = new HashMap<>(2, 1);
    result.put("httpStatus", httpStatus);

    String msg = "{\"code\":" + httpStatus.value() + ",\"message\": \"" + body + "\"}";
    result.put("body", msg);

    // 错误日志
    ServerHttpRequest request = serverWebExchange.getRequest();
    log.error("[全局系统异常]\r\n请求路径:{}\r\n异常记录:{}", request.getPath(), throwable.getMessage());

    if (serverWebExchange.getResponse().isCommitted()) {
        return Mono.error(throwable);
    }
    exceptionHandlerResult.set(result);
    ServerRequest newRequest = ServerRequest.create(serverWebExchange, this.messageReaders);
    return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
            .switchIfEmpty(Mono.error(throwable))
            .flatMap((handler) -> handler.handle(newRequest))
            .flatMap((response) -> write(serverWebExchange, response));
}
 
Example 2
Source File: CFExceptionMapper.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@ExceptionHandler
public ResponseEntity<String> handleException(Exception e) {
    HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
    String message = e.getMessage();

    if (e instanceof CloudOperationException) {
        status = HttpStatus.BAD_GATEWAY;
    }
    if (e instanceof ContentException || e instanceof IllegalArgumentException) {
        status = HttpStatus.BAD_REQUEST;
    }
    if (e instanceof NotFoundException) {
        status = HttpStatus.NOT_FOUND;
    }
    if (e instanceof ConflictException) {
        status = HttpStatus.CONFLICT;
    }
    if (e instanceof ResponseStatusException) {
        ResponseStatusException rse = (ResponseStatusException) e;
        status = rse.getStatus();
        message = rse.getReason();
    }
    if (e instanceof SQLException || e instanceof PersistenceException) {
        message = Messages.TEMPORARY_PROBLEM_WITH_PERSISTENCE_LAYER;
    }

    LOGGER.error(Messages.ERROR_EXECUTING_REST_API_CALL, e);
    return ResponseEntity.status(status)
                         .body(message);
}
 
Example 3
Source File: ExceptionHandle.java    From microservice-recruit with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    /**
     * 按照异常类型进行处理
     */
    HttpStatus httpStatus;
    String body;
    if (ex instanceof MyException) {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        body = JSONObject.toJSONString(ResponseMessage.error(((MyException) ex).getCode(), ex.getMessage()));
    } else if (ex instanceof NotFoundException) {
        httpStatus = HttpStatus.NOT_FOUND;
        body = JSONObject.toJSONString(ResponseMessage.error(404, ex.getMessage()));
    } else if (ex instanceof ResponseStatusException) {
        ResponseStatusException responseStatusException = (ResponseStatusException) ex;
        httpStatus = responseStatusException.getStatus();
        body = JSONObject.toJSONString(ResponseMessage.error(500, ex.getMessage()));
    } else {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        body = JSONObject.toJSONString(ResponseMessage.error(500, ex.getMessage()));
    }
    /**
     * 封装响应体,此body可修改为自己的jsonBody
     */
    Map<String, Object> result = new HashMap<>(2, 1);
    result.put("httpStatus", httpStatus);
    result.put("body", body);
    /**
     * 错误记录
     */
    ServerHttpRequest request = exchange.getRequest();
    log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", request.getPath().pathWithinApplication().value(), ex.getMessage());
    ex.printStackTrace();
    /**
     * 参考AbstractErrorWebExceptionHandler
     */
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    exceptionHandlerResult.set(result);
    ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
    return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
            .switchIfEmpty(Mono.error(ex))
            .flatMap((handler) -> handler.handle(newRequest))
            .flatMap((response) -> write(exchange, response));

}
 
Example 4
Source File: ExceptionHandle.java    From microservice-recruit with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    /**
     * 按照异常类型进行处理
     */
    HttpStatus httpStatus;
    String body;
    if (ex instanceof MyException) {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        body = JSONObject.toJSONString(ResponseMessage.error(((MyException) ex).getCode(), ex.getMessage()));
    } else if (ex instanceof NotFoundException) {
        httpStatus = HttpStatus.NOT_FOUND;
        body = JSONObject.toJSONString(ResponseMessage.error(404, ex.getMessage()));
    } else if (ex instanceof ResponseStatusException) {
        ResponseStatusException responseStatusException = (ResponseStatusException) ex;
        httpStatus = responseStatusException.getStatus();
        body = JSONObject.toJSONString(ResponseMessage.error(500, ex.getMessage()));
    } else {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        body = JSONObject.toJSONString(ResponseMessage.error(500, ex.getMessage()));
    }
    /**
     * 封装响应体,此body可修改为自己的jsonBody
     */
    Map<String, Object> result = new HashMap<>(2, 1);
    result.put("httpStatus", httpStatus);
    result.put("body", body);
    /**
     * 错误记录
     */
    ServerHttpRequest request = exchange.getRequest();
    log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", request.getPath().pathWithinApplication().value(), ex.getMessage());
    ex.printStackTrace();
    /**
     * 参考AbstractErrorWebExceptionHandler
     */
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    exceptionHandlerResult.set(result);
    ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
    return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
            .switchIfEmpty(Mono.error(ex))
            .flatMap((handler) -> handler.handle(newRequest))
            .flatMap((response) -> write(exchange, response));

}