Skip to content

Commit

Permalink
[fix] 에러 메시지 json으로 파싱
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Oct 27, 2023
1 parent 10ebbe6 commit 859169a
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.farmusgateway.filter;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
Expand All @@ -21,6 +23,8 @@
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
Expand Down Expand Up @@ -132,19 +136,31 @@ public String decode(String token) {
}

// Mono, Flux -> Spring WebFlux

private Mono<Void> onError(ServerWebExchange exchange, String error, HttpStatus httpStatus) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(httpStatus);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);

String errorMessage = "{\"error\": \"" + error + "\"}";
DataBuffer buffer = response.bufferFactory().wrap(errorMessage.getBytes(StandardCharsets.UTF_8));
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("error", error);

ObjectMapper objectMapper = new ObjectMapper();
byte[] errorResponseBytes;
try {
errorResponseBytes = objectMapper.writeValueAsBytes(errorResponse);
} catch (JsonProcessingException e) {
errorResponseBytes = "{\"error\": \"Internal Server Error\"}".getBytes(StandardCharsets.UTF_8);
}

DataBuffer buffer = response.bufferFactory().wrap(errorResponseBytes);

log.error(error);

return response.writeWith(Mono.just(buffer)).then(response.setComplete());
}


@Data
public static class Config {

Expand Down

0 comments on commit 859169a

Please sign in to comment.