-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($APIGateway): add global exception handler
- Loading branch information
1 parent
963f135
commit 7bb6ffa
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...src/main/java/com/jmsoftware/maf/apigateway/universal/handler/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.jmsoftware.maf.apigateway.universal.handler; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.jmsoftware.maf.common.bean.ResponseBodyBean; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.server.ResponseStatusException; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* <h1>GlobalExceptionHandler</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com | ||
* @date 12/23/20 7:45 AM | ||
**/ | ||
@Slf4j | ||
@Order(-1) | ||
@Component | ||
@RequiredArgsConstructor | ||
public class GlobalExceptionHandler implements ErrorWebExceptionHandler { | ||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
@SuppressWarnings("NullableProblems") | ||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) { | ||
ServerHttpResponse response = exchange.getResponse(); | ||
|
||
if (response.isCommitted()) { | ||
return Mono.error(ex); | ||
} | ||
|
||
// header set | ||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); | ||
if (ex instanceof ResponseStatusException) { | ||
response.setStatusCode(((ResponseStatusException) ex).getStatus()); | ||
} | ||
|
||
return response.writeWith(Mono.fromSupplier(() -> { | ||
DataBufferFactory bufferFactory = response.bufferFactory(); | ||
final var responseBody = ResponseBodyBean.builder().timestamp(new Date()).status(HttpStatus.INTERNAL_SERVER_ERROR.value()).message(ex.getMessage()).build(); | ||
try { | ||
return bufferFactory.wrap(objectMapper.writeValueAsBytes(responseBody)); | ||
} catch (JsonProcessingException e) { | ||
log.warn("Error writing response", ex); | ||
return bufferFactory.wrap(new byte[0]); | ||
} | ||
})); | ||
} | ||
} |