Java Code Examples for org.springframework.web.client.UnknownHttpStatusCodeException

Following code examples demonstrate how to use org.springframework.web.client.UnknownHttpStatusCodeExceptionfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.client.UnknownHttpStatusCodeExceptionand various code implementation of this class.

    protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
        switch (statusCode.series()) {
            case CLIENT_ERROR:
                throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), this.getResponseBody(response), this.getCharset(response));
            case SERVER_ERROR:
                throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), this.getResponseBody(response), this.getCharset(response));
            default:
                throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(), response.getHeaders(), this.getResponseBody(response), this.getCharset(response));
        }
    } 


	private HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
		HttpStatus statusCode;
		try {
			statusCode = response.getStatusCode();
		}
		catch (IllegalArgumentException ex) {
			throw new UnknownHttpStatusCodeException(response.getRawStatusCode(),
					response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
		}
		return statusCode;
	} 

        catch (IllegalArgumentException ex) {
            throw new UnknownHttpStatusCodeException(response.getRawStatusCode(),
                    response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
        } 

    protected void doHealthCheck(Health.Builder builder) {

        try {
            restTemplate.headForHeaders(checkUrl);
            builder.up();
        } catch (HttpServerErrorException | UnknownHttpStatusCodeException | ResourceAccessException e) {
            builder.down(e);
        }
    } 

	public Product getProduct(String id) {
		
		Product product;
		
		try {
			
			RestTemplate restTemplate = new RestTemplate();
			restTemplate.getInterceptors().add(getBasicAuthInterceptor());
			product = restTemplate.getForObject(_apiBaseUrl + "/" + id, Product.class);
			
		} catch (HttpClientErrorException e) {
			if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
				throw new ProductNotFoundException(id);
			} else {
				throw e;
			}
		}  catch (HttpServerErrorException | UnknownHttpStatusCodeException e) {
			throw e;
		}
		
		return product;
	} 

	public List<Product> getAllProducts() {

		List<Product> productList;
		
		try {
			
			RestTemplate restTemplate = new RestTemplate();
			restTemplate.getInterceptors().add(getBasicAuthInterceptor());
			ResponseEntity<Product[]> responseEntity = restTemplate.getForEntity(_apiBaseUrl, Product[].class);
			Product[] productArr = responseEntity.getBody();
			productList = Arrays.asList(productArr);
			
		} catch (HttpClientErrorException e) {
			if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
				throw new NoProductsFoundException();
			} else {
				throw e;
			}
		}  catch (HttpServerErrorException | UnknownHttpStatusCodeException e) {
			throw e;
		}
		
		return productList;
		
	} 

    private HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
        HttpStatus statusCode;
        try {
            statusCode = response.getStatusCode();
        } catch (IllegalArgumentException ex) {
            throw new UnknownHttpStatusCodeException(response.getRawStatusCode(),
                    response.getStatusText(), response.getHeaders(), this.getResponseBody(response), this.getCharset(response));
        }
        return statusCode;
    } 

    public Result handle(UnknownHttpStatusCodeException e) {
        LOGGER.error(e.getMessage(), e);
        return new Result(ResultEnum.SERVER_ERROR);
    } 

    public Health health() {
        ResponseEntity<String> responseEntity;

        try {
            responseEntity = restTemplate.getForEntity(url + endpoint, String.class);

        } catch (ResourceAccessException rae) {
            log.trace(rae.getMessage(), rae);
            return getHealthWithDownStatus(url, rae.getMessage(), "ResourceAccessException");
        } catch (HttpStatusCodeException hsce) {
            log.trace(hsce.getMessage(), hsce);
            return getHealthWithDownStatus(url, hsce.getMessage(),
                    "HttpStatusCodeException - HTTP Status: " + hsce.getStatusCode().value());
        } catch (UnknownHttpStatusCodeException uhsce) {
            log.trace(uhsce.getMessage(), uhsce);
            return getHealthWithDownStatus(url, uhsce.getMessage(), "UnknownHttpStatusCodeException - " + uhsce.getStatusText());
        }

        if (responseEntity != null && !responseEntity.getStatusCode().equals(HttpStatus.OK)) {
            return getHealthWithDownStatus(url, "HTTP Status code not 200", "HTTP Status: " + responseEntity.getStatusCodeValue());
        }

        return getHealthWithUpStatus(url);

    } 

    public <T> Supplier<T> call(Supplier<T> supplier) {
        return () -> {
            try {
                return supplier.get();
            } catch (HttpClientErrorException clientEx) {
                log.error("Client Exception calling ER Adapter Service HttpStatus: {}", clientEx.getStatusText());
                throw new ExternalServiceException("Client Exception calling ER Adapter Service ", clientEx);
            } catch (HttpServerErrorException serverEx) {
                log.error("HttpStatus: {}", serverEx.getStatusText());
                log.error(serverEx.getResponseBodyAsString());
                throw new ExternalServiceException("Server Exception calling ER Adapter Service ", serverEx);
            } catch (UnknownHttpStatusCodeException unknownEx) {
                log.error("Unknown Exception thrown with Status: {}", unknownEx.getStatusText());
                throw new ExternalServiceException("Unknown Http Exception ER Adapter Service", unknownEx);
            } catch (Exception e) {
                log.error("Unexpected exception from calling ER Adapter Service  Message: {}", e.getMessage());
                throw new ExternalServiceException("Unexpected Exception", e);
            }
        };
    } 

	protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException {
		try {
			return response.getStatusCode();
		}
		catch (IllegalArgumentException ex) {
			throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),
					response.getHeaders(), getResponseBody(response), getCharset(response));
		}
	} 

        catch (IllegalArgumentException ex) {
            throw new UnknownHttpStatusCodeException(response.getRawStatusCode(),
                    response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
        } 

  @Override
  protected void handleError( ClientHttpResponse response, HttpStatus statusCode ) throws IOException
  {
    switch( statusCode.series() )
    {
      case CLIENT_ERROR:
        if( statusCode == HttpStatus.NOT_FOUND )
          throw new ResourceNotFoundException();
          throw new ChargifyException( readInputStream( response.getBody() ) );
        else
          throw objectMapper.readValue( response.getBody(), ChargifyError.class ).exception();
      case SERVER_ERROR:
        throw new HttpServerErrorException( statusCode, response.getStatusText(),
                                            response.getHeaders(), getResponseBody( response ), getCharset( response ) );
      default:
        throw new UnknownHttpStatusCodeException( statusCode.value(), response.getStatusText(),
                                                  response.getHeaders(), getResponseBody( response ), getCharset( response ) );
    }
  } 

    public boolean hasError(ClientHttpResponse response) throws IOException {
        boolean hasError = false;

        try {
            hasError = super.hasError(response);
        } catch (UnknownHttpStatusCodeException e) {
            hasError = true;
        }

        return hasError;
    } 

	public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {

		if (statusCode.series() == HttpStatus.Series.CLIENT_ERROR) {
			throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(),
					getResponseBody(response), getCharset(response));
		} else if (statusCode.series() == HttpStatus.Series.SERVER_ERROR) {
			throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(),
					getResponseBody(response), getCharset(response));
		} else {
			try {
				HttpStatus.Series.valueOf(statusCode);
				super.handleError(response, statusCode);
			} catch (Exception e) {
				throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(),
						response.getHeaders(), getResponseBody(response), getCharset(response));
			}
		}
	} 

Advertisement
Javadoc
Exception thrown when an unknown (or custom) HTTP status code is received. @author Rossen Stoyanchev

@since 3.2

Read More
Advertisement