Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): Improve error handling for WebClient #1131

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,11 +30,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import retrofit.RetrofitError;
import retrofit.client.Header;

Expand Down Expand Up @@ -89,6 +94,36 @@ public void handleIllegalStateException(
handleResponseStatusAnnotatedException(e, response);
}

@ExceptionHandler(WebClientResponseException.class)
public ResponseEntity<UpstreamRequestError> handleWebClientResponseException(
WebClientResponseException e) {
HttpStatus statusCode = e.getStatusCode();
var builder = ResponseEntity.status(statusCode);
var error = new UpstreamRequestError();
error.setStatus(e.getRawStatusCode());
error.setError(e.getStatusText());
HttpRequest request = e.getRequest();
if (request != null) {
error.setUri(request.getURI());
error.setMethod(request.getMethod());
}
HttpHeaders headers = e.getHeaders();
String eTag = headers.getETag();
if (eTag != null) {
builder.eTag(eTag);
}
long lastModified = headers.getLastModified();
if (lastModified != -1) {
builder.lastModified(lastModified);
}
MediaType contentType = headers.getContentType();
if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
error.setBody(e.getResponseBodyAsString());
}
error.setMessage(exceptionMessageDecorator.decorate(e, e.getMessage()));
return builder.body(error);
}

@ExceptionHandler(RetrofitError.class)
public void handleRetrofitError(
RetrofitError e, HttpServletResponse response, HttpServletRequest request)
Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright 2023 Apple Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.kork.web.exceptions;

import com.fasterxml.jackson.annotation.JsonRawValue;
import java.net.URI;
import java.time.Instant;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpMethod;

@Getter
@Setter
public class UpstreamRequestError {
private int status;
private String error;
private Instant timestamp = Instant.now();
private HttpMethod method;
private URI uri;
private String message;
@JsonRawValue private String body;
}