下面列出了org.springframework.http.HttpStatus#BAD_GATEWAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void testOnStatus(Throwable expected,
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
HttpStatus errorStatus = HttpStatus.BAD_GATEWAY;
this.server.enqueue(new MockResponse()
.setResponseCode(errorStatus.value())
.setHeader("Content-Type", "application/json")
.setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5));
Mono<String> mono = this.webClient.get()
.uri("/json").accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(status -> status.equals(errorStatus), exceptionFunction)
.bodyToMono(String.class);
StepVerifier.create(mono).expectErrorSatisfies(actual -> assertSame(expected, actual)).verify(DELAY);
assertEquals(1, this.server.getRequestCount());
}
@ExceptionHandler(TechnicalRuntimeException.class)
protected ResponseEntity<Response<?>> handleTechnicalRuntimeException(TechnicalRuntimeException tre) {
if (tre.getCause() instanceof BehaviorAwareException) {
return handleBehaviorAwareException((BehaviorAwareException) tre.getCause());
}
if (tre.getCause() instanceof BusinessRuntimeException) {
return handleBusinessRuntimeException((BusinessRuntimeException) tre.getCause());
}
if (tre.getCause() instanceof HttpBusinessException) {
return handleHttpBusinessException((HttpBusinessException) tre.getCause());
}
EXC_LOGGER.error("[P] Presentation Layer Exception: {}", tre.getLocalizedMessage(), tre);
return new ResponseEntity<>(Response.newBuilder()
.withMessage(tre.getMessage())
.withMessageKey(tre.getMessageKey())
.withHttpStatus(String.valueOf(HttpStatus.BAD_GATEWAY.value()))
.withObj(tre.getData())
.build(),
HttpStatus.BAD_GATEWAY
);
}
private void testOnStatus(Throwable expected,
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
HttpStatus errorStatus = HttpStatus.BAD_GATEWAY;
this.server.enqueue(new MockResponse()
.setResponseCode(errorStatus.value())
.setHeader("Content-Type", "application/json")
.setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5));
Mono<String> mono = this.webClient.get()
.uri("/json").accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(status -> status.equals(errorStatus), exceptionFunction)
.bodyToMono(String.class);
StepVerifier.create(mono).expectErrorSatisfies(actual -> assertSame(expected, actual)).verify(DELAY);
assertEquals(1, this.server.getRequestCount());
}
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public BaseResponse handleNoHandlerFoundException(NoHandlerFoundException e) {
BaseResponse<?> baseResponse = handleBaseException(e);
HttpStatus status = HttpStatus.BAD_GATEWAY;
baseResponse.setStatus(status.value());
baseResponse.setMessage(status.getReasonPhrase());
return baseResponse;
}
private void processServiceCreationFailure(ProcessContext context, CloudServiceInstanceExtended service, CloudOperationException e) {
if (!service.isOptional()) {
String detailedDescription = MessageFormat.format(Messages.ERROR_CREATING_SERVICE, service.getName(), service.getLabel(),
service.getPlan(), e.getDescription());
if (e.getStatusCode() == HttpStatus.BAD_GATEWAY) {
context.setVariable(Variables.SERVICE_OFFERING, service.getLabel());
throw new CloudServiceBrokerException(e.getStatusCode(), e.getStatusText(), detailedDescription);
}
throw new CloudControllerException(e.getStatusCode(), e.getStatusText(), detailedDescription);
}
getStepLogger().warn(MessageFormat.format(Messages.COULD_NOT_EXECUTE_OPERATION_OVER_OPTIONAL_SERVICE, service.getName()), e,
ExceptionMessageTailMapper.map(configuration, CloudComponents.SERVICE_BROKERS, service.getLabel()));
}
public <T> T executeServiceOperation(CloudServiceInstanceExtended service, Supplier<T> serviceOperation, StepLogger stepLogger) {
try {
return serviceOperation.get();
} catch (CloudOperationException e) {
if (!service.isOptional()) {
if (e.getStatusCode() == HttpStatus.BAD_GATEWAY) {
throw new CloudServiceBrokerException(e);
}
throw new CloudControllerException(e);
}
stepLogger.warn(e, Messages.COULD_NOT_EXECUTE_OPERATION_OVER_OPTIONAL_SERVICE, service.getName());
return null;
}
}
private CloudOperationException evaluateCloudOperationException(ProcessContext context, CloudOperationException e, String serviceName,
String label) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
context.getStepLogger()
.warn(MessageFormat.format(Messages.COULD_NOT_DELETE_SERVICE, serviceName), e,
ExceptionMessageTailMapper.map(configuration, CloudComponents.SERVICE_BROKERS, label));
return null;
}
if (e.getStatusCode() == HttpStatus.BAD_GATEWAY) {
context.setVariable(Variables.SERVICE_OFFERING, label);
return new CloudServiceBrokerException(e);
}
return new CloudControllerException(e);
}
@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);
}
@RequestMapping(value = "/**", produces = {"text/xml"})
@ResponseBody
public ResponseEntity<?> getResponse() throws InterruptedException, ExecutionException {
if (returnError.get(count.getAndIncrement())) {
return new ResponseEntity<Void>(HttpStatus.BAD_GATEWAY);
}
ReceiveMessageResponse receiveMessageResponse = asyncClient.receiveMessage(receiveMessageRequest).get();
return ResponseEntity.ok(mockedXmlResponse(receiveMessageResponse.messages().get(0)));
}
BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public Object gateway(RuntimeException e) {
return null;
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public Object gateway(RuntimeException e) {
return null;
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public Object gateway(RuntimeException e) {
return null;
}
BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
}
@SneakyThrows
private boolean isBadGateway(@Nullable final ClientHttpResponse response) {
return response != null && response.getStatusCode() == HttpStatus.BAD_GATEWAY;
}
@SneakyThrows
private boolean isBadGateway(@Nullable final ClientHttpResponse response) {
return response != null && response.getStatusCode() == HttpStatus.BAD_GATEWAY;
}
/**
* Fallback method for getRestaurant()
*
* @param restaurantId
*/
public ResponseEntity<Restaurant> defaultRestaurant(
@PathVariable int restaurantId) {
return new ResponseEntity<>(HttpStatus.BAD_GATEWAY);
}
/**
* Fallback method for getRestaurant()
*
* @param restaurantId
*/
public ResponseEntity<Restaurant> defaultRestaurant(
@PathVariable int restaurantId) {
return new ResponseEntity<>(HttpStatus.BAD_GATEWAY);
}
/**
* Fallback method for getRestaurant()
*
* @param restaurantId
* @return
*/
public ResponseEntity<Restaurant> defaultRestaurant(
@PathVariable int restaurantId) {
return new ResponseEntity<>(null, HttpStatus.BAD_GATEWAY);
}
/**
* Fallback method for getRestaurant()
*
* @param restaurantId
* @return
*/
public ResponseEntity<Restaurant> defaultRestaurant(
@PathVariable int restaurantId) {
return new ResponseEntity<>(null, HttpStatus.BAD_GATEWAY);
}