Skip to content

Commit

Permalink
Merge pull request #71 from YAPP-Github/feature/MAFOO-169
Browse files Browse the repository at this point in the history
[MAFOO-169] feat: QR ์‚ฌ์ง„ ์˜ˆ์™ธ ๊ด€๋ จ ์Šฌ๋ž™ ์•Œ๋ฆผ์„ ์ถ”๊ฐ€ํ–ˆ์–ด์š”
  • Loading branch information
gmkim20713 authored Oct 26, 2024
2 parents fc132fd + 20b06b7 commit a4d047c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package kr.mafoo.photo.config;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebExchangeDecorator;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public class RequestBodyCachingFilter implements WebFilter {

// TODO: ์ถ”ํ›„ ์ •๋ฆฌ ํ•„์š”

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return DataBufferUtils.join(exchange.getRequest().getBody())
.flatMap(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);

ServerHttpRequestDecorator decoratedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
@Override
public Flux<DataBuffer> getBody() {
return Flux.just(exchange.getResponse().bufferFactory().wrap(bytes));
}
};

ServerWebExchangeDecorator decoratedExchange = new ServerWebExchangeDecorator(exchange) {
@Override
public ServerHttpRequest getRequest() {
return decoratedRequest;
}
};

return chain.filter(decoratedExchange);
});
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kr.mafoo.photo.controller.dto.response.ErrorResponse;
import kr.mafoo.photo.exception.DomainException;
import kr.mafoo.photo.exception.ErrorCode;
import kr.mafoo.photo.exception.PhotoBrandNotExistsException;
import kr.mafoo.photo.service.SlackService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
Expand Down Expand Up @@ -61,6 +62,26 @@ public ResponseEntity<ErrorResponse> validException(Exception ex) {
.body(response);
}

@ExceptionHandler(PhotoBrandNotExistsException.class)
public Mono<ResponseEntity<ErrorResponse>> handlePhotoBrandNotExistsException(ServerWebExchange exchange, PhotoBrandNotExistsException exception) {
String method = extractMethod(exchange);
String userAgent = extractUserAgent(exchange);
String fullPath = extractURI(exchange);
String originIp = extractOriginIp(exchange);

return extractRequestBody(exchange).flatMap(requestBody -> {
logException(method, fullPath, originIp, userAgent, exception);

return slackService.sendQrRelatedErrorNotification(
method, fullPath, requestBody, originIp, userAgent, exception.getMessage()
).then(Mono.just(
ResponseEntity
.badRequest()
.body(ErrorResponse.fromErrorCode(exception.getErrorCode()))
));
});
}

@ExceptionHandler(ResponseStatusException.class)
public Mono<ResponseEntity<String>> handleResponseStatusException(ServerWebExchange exchange, ResponseStatusException exception) {
return handleExceptionInternal(exchange, exception, (HttpStatus) exception.getStatusCode());
Expand Down
3 changes: 2 additions & 1 deletion photo-service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ slack:
webhook:
token: ${SLACK_TOKEN}
channel:
error: ${SLACK_ERROR_CHANNEL}
error: ${SLACK_ERROR_CHANNEL}
qr: ${SLACK_QR_ERROR_CHANNEL}

0 comments on commit a4d047c

Please sign in to comment.