Java Code Examples for org.springframework.http.HttpStatus#BAD_GATEWAY

The following examples show how to use org.springframework.http.HttpStatus#BAD_GATEWAY . 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: WebClientDataBufferAllocatingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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());
}
 
Example 2
Source File: AbstractWebController.java    From org.openwms with Apache License 2.0 6 votes vote down vote up
@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
    );
}
 
Example 3
Source File: WebClientDataBufferAllocatingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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());
}
 
Example 4
Source File: SqsMessageQueueReceiverEndpointIntegrationTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example 5
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 6
Source File: ServiceRemover.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
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);

}
 
Example 7
Source File: ServiceOperationExecutor.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 8
Source File: CreateServiceStep.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
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()));
}
 
Example 9
Source File: ControllerExceptionHandler.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: FailsafePluginRetryListenerTest.java    From riptide with MIT License 4 votes vote down vote up
@SneakyThrows
private boolean isBadGateway(@Nullable final ClientHttpResponse response) {
    return response != null && response.getStatusCode() == HttpStatus.BAD_GATEWAY;
}
 
Example 11
Source File: FailsafePluginRetriesTest.java    From riptide with MIT License 4 votes vote down vote up
@SneakyThrows
private boolean isBadGateway(@Nullable final ClientHttpResponse response) {
    return response != null && response.getStatusCode() == HttpStatus.BAD_GATEWAY;
}
 
Example 12
Source File: HttpServerErrorException.java    From java-technology-stack with MIT License 4 votes vote down vote up
BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
	super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
}
 
Example 13
Source File: MyExceptionHandler.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public Object gateway(RuntimeException e) {
	return null;
}
 
Example 14
Source File: MyExceptionHandler.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public Object gateway(RuntimeException e) {
	return null;
}
 
Example 15
Source File: MyExceptionHandler.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public Object gateway(RuntimeException e) {
	return null;
}
 
Example 16
Source File: HttpServerErrorException.java    From spring-analysis-note with MIT License 4 votes vote down vote up
BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
	super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
}
 
Example 17
Source File: RestaurantServiceAPI.java    From Mastering-Microservices-with-Java-9-Second-Edition with MIT License 2 votes vote down vote up
/**
 * Fallback method for getRestaurant()
 *
 * @param restaurantId
 * @return
 */
public ResponseEntity<Restaurant> defaultRestaurant(
        @PathVariable int restaurantId) {
    return new ResponseEntity<>(null, HttpStatus.BAD_GATEWAY);
}
 
Example 18
Source File: RestaurantServiceAPI.java    From Mastering-Microservices-with-Java-9-Second-Edition with MIT License 2 votes vote down vote up
/**
 * Fallback method for getRestaurant()
 *
 * @param restaurantId
 * @return
 */
public ResponseEntity<Restaurant> defaultRestaurant(
        @PathVariable int restaurantId) {
    return new ResponseEntity<>(null, HttpStatus.BAD_GATEWAY);
}
 
Example 19
Source File: RestaurantServiceAPI.java    From Mastering-Microservices-with-Java-Third-Edition with MIT License 2 votes vote down vote up
/**
 * Fallback method for getRestaurant()
 *
 * @param restaurantId
 */
public ResponseEntity<Restaurant> defaultRestaurant(
    @PathVariable int restaurantId) {
  return new ResponseEntity<>(HttpStatus.BAD_GATEWAY);
}
 
Example 20
Source File: RestaurantServiceAPI.java    From Mastering-Microservices-with-Java-Third-Edition with MIT License 2 votes vote down vote up
/**
 * Fallback method for getRestaurant()
 *
 * @param restaurantId
 */
public ResponseEntity<Restaurant> defaultRestaurant(
    @PathVariable int restaurantId) {
  return new ResponseEntity<>(HttpStatus.BAD_GATEWAY);
}