Skip to content

Commit

Permalink
perf($APIGateway): handle WebClientResponseException
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jan 28, 2021
1 parent 4f5a20d commit 8c4cd5a
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import lombok.val;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
* <h1>GlobalExceptionHandler</h1>
* <p>
Expand All @@ -41,21 +43,21 @@ public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
log.error("Exception occurred when [{}] requested access. Exception message: {}. Request URL: [{}] {}",
RequestUtil.getRequesterIpAndPort(request), ex.getMessage(), request.getMethod(), request.getURI(),
ex);
ServerHttpResponse response = exchange.getResponse();
val response = exchange.getResponse();
if (response.isCommitted()) {
return Mono.error(ex);
}
// Set HTTP header, major browsers like Chrome now comply with the specification and interpret correctly
// UTF-8 special characters without requiring a charset=UTF-8 parameter.
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
final var responseBody = setResponseBody(response, ex);
val bufferFactory = response.bufferFactory();
val responseBody = setResponseBody(response, ex);
try {
return bufferFactory.wrap(objectMapper.writeValueAsBytes(responseBody));
} catch (JsonProcessingException e) {
log.warn("Exception occurred when writing response", e);
return bufferFactory.wrap(new byte[0]);
return bufferFactory.wrap(e.getMessage().getBytes(StandardCharsets.UTF_8));
}
}));
}
Expand All @@ -75,6 +77,15 @@ private ResponseBodyBean<?> setResponseBody(ServerHttpResponse response, Throwab
HttpStatus status = HttpStatus.valueOf(((SecurityException) ex).getCode());
response.setStatusCode(status);
return ResponseBodyBean.ofStatus(status, ex.getMessage());
} else if (ex instanceof WebClientResponseException) {
val exception = (WebClientResponseException) ex;
response.setStatusCode(exception.getStatusCode());
try {
return objectMapper.readValue(exception.getResponseBodyAsString(), ResponseBodyBean.class);
} catch (JsonProcessingException e) {
log.warn("Exception occurred when writing response", e);
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}
}
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
Expand Down

0 comments on commit 8c4cd5a

Please sign in to comment.