/** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}. * This is a shortcut for checking the value of {@link #series()}. * @since 5.0 * @see #is4xxClientError() * @see #is5xxServerError() */ public boolean isError() { return (is4xxClientError() || is5xxServerError()); }
/** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}. * This is a shortcut for checking the value of {@link #series()}. * @since 5.0 * @see #is4xxClientError() * @see #is5xxServerError() */ public boolean isError() { return (is4xxClientError() || is5xxServerError()); }
void terminateSpan(@Nullable ClientResponse clientResponse, @Nullable Throwable throwable) { if (clientResponse == null || clientResponse.statusCode() == null) { if (log.isDebugEnabled()) { log.debug("No response was returned. Will close the span [" + this.span + "]"); } handleReceive(this.span, this.ws, clientResponse, throwable); return; } boolean error = clientResponse.statusCode().is4xxClientError() || clientResponse.statusCode().is5xxServerError(); if (error) { if (log.isDebugEnabled()) { log.debug( "Non positive status code was returned from the call. Will close the span [" + this.span + "]"); } throwable = new RestClientException("Status code of the response is [" + clientResponse.statusCode().value() + "] and the reason is [" + clientResponse.statusCode().getReasonPhrase() + "]"); } handleReceive(this.span, this.ws, clientResponse, throwable); }
@Test public void test400_500Errors() throws Exception { handler.setErrorAtLevel(HttpStatus.Series.CLIENT_ERROR); for (HttpStatus status : HttpStatus.values()) { ClientHttpResponse response = responses.get(status); if (status.is5xxServerError() || status.is4xxClientError()) { Assert.assertTrue(handler.hasError(response)); } else { Assert.assertFalse(handler.hasError(response)); } } }
/** * Creates an {@code outcome} tag based on the response status of the given * {@code exchange}. * @param exchange the exchange * @return the outcome tag derived from the response status * @since 2.1.0 */ public static Tag outcome(ServerWebExchange exchange) { HttpStatus status = exchange.getResponse().getStatusCode(); if (status != null) { if (status.is1xxInformational()) { return OUTCOME_INFORMATIONAL; } if (status.is2xxSuccessful()) { return OUTCOME_SUCCESS; } if (status.is3xxRedirection()) { return OUTCOME_REDIRECTION; } if (status.is4xxClientError()) { return OUTCOME_CLIENT_ERROR; } return OUTCOME_SERVER_ERROR; } return OUTCOME_UNKNOWN; }
/** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}. * This is a shortcut for checking the value of {@link #series()}. * @since 5.0 * @see #is4xxClientError() * @see #is5xxServerError() */ public boolean isError() { return (is4xxClientError() || is5xxServerError()); }
private ResponseEntity<Messages> handleInternal(Throwable throwable, Messages messages, HttpStatus httpStatus) { if (httpStatus == HttpStatus.NOT_FOUND) { log.info("Exception {} from Controller: {}", throwable.getClass(), throwable.getMessage()); } else if (httpStatus.is4xxClientError()) { log.warn("Exception {} from Controller: {}", throwable.getClass(), NestedExceptionUtils.buildMessage(throwable.getMessage(), throwable.getCause())); } else { log.error("Exception {} from Controller: {}", throwable); } if (messages == null) { return new ResponseEntity<>(httpStatus); } else { return new ResponseEntity<>(messages, httpStatus); } }
/** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}. * This is a shortcut for checking the value of {@link #series()}. * @since 5.0 * @see #is4xxClientError() * @see #is5xxServerError() */ public boolean isError() { return (is4xxClientError() || is5xxServerError()); }
@Override protected boolean hasError(HttpStatus statusCode) { /** * When the Eureka server restarts and a client tries to sent a heartbeat the server * will respond with a 404. By default RestTemplate will throw an exception in this case. * What we want is to return the 404 to the upstream code so it will send another registration * request to the server. */ if(statusCode.is4xxClientError()) { return false; } return super.hasError(statusCode); } }
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); } }
default void log( @SuppressWarnings("UnusedParameters") final Throwable throwable, @SuppressWarnings("UnusedParameters") final Problem problem, @SuppressWarnings("UnusedParameters") final NativeWebRequest request, final HttpStatus status) { if (status.is4xxClientError()) { LOG.warn("{}: {}", status.getReasonPhrase(), throwable.getMessage()); } else if (status.is5xxServerError()) { LOG.error(status.getReasonPhrase(), throwable); } }
@Override public Object getCustom(String customPath, String context, Class clazz) throws NotFoundException { final Map<String, Object> params = context == null ? null : ImmutableMap.of(CONTEXT_, context); try { return doExchange1(customPath, HttpMethod.GET, clazz, new Object[0], params, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new NotFoundException(e); } else { throw e; } } }
@Override public User getUser(long userId, String context) throws UserNotFoundException { final Map<String, Object> params = context == null ? null : ImmutableMap.of(CONTEXT_, context); try { return doExchange1(Request.USER, HttpMethod.GET, User.class, forExpand(userId), params, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new UserNotFoundException(e); } else { throw e; } } }
public Post getPost(Long id, String postTypeName, String context) throws PostNotFoundException { try { return doExchange1(postTypeName, HttpMethod.GET, Post.class, forExpand(id), ImmutableMap.of(CONTEXT_, context), null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new PostNotFoundException(e); } else { throw e; } } }
@Override public Term getTerm(String taxonomy, Long id) throws TermNotFoundException { try { return doExchange1(Request.TERM, HttpMethod.GET, Term.class, forExpand(taxonomy, id), null, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new TermNotFoundException(e); } else { throw e; } } }
private static boolean isWarn(Integer statusCode) { return HttpStatus.valueOf(statusCode).is3xxRedirection() || HttpStatus.valueOf(statusCode).is4xxClientError(); }
@Override public Term getTag(Long id) throws TermNotFoundException { try { return doExchange1(Request.TAG, HttpMethod.GET, Term.class, forExpand(id), null, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new TermNotFoundException(e); } else { throw e; } } }
@Override public Term deleteCategory(Term categoryTerm, boolean force) throws TermNotFoundException { try { Map<String, Object> queryParams = force ? ImmutableMap.of("force", true) : null; return doExchange1(Request.CATEGORY, HttpMethod.DELETE, Term.class, forExpand(categoryTerm.getId()), queryParams, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new TermNotFoundException(e); } else { throw e; } } }
@Override public Term deleteTerm(String taxonomy, Term term) throws TermNotFoundException { try { return doExchange1(Request.TERM, HttpMethod.DELETE, Term.class, forExpand(taxonomy, term.getId()), null, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new TermNotFoundException(e); } else { throw e; } } }
@Override public Term getPostTag(Post post, Term tagTerm) throws TermNotFoundException { try { return doExchange1(Request.POST_TERM, HttpMethod.GET, Term.class, forExpand(post.getId(), TAGS, tagTerm.getId()), null, null).getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) { throw new TermNotFoundException(e); } else { throw e; } } }