Skip to content

Commit

Permalink
feat: ExceptionHandlerFilter 추가 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
devmizz committed May 31, 2024
1 parent 5547480 commit c346b6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.IOException;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.example.exception.GlobalError;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

Expand All @@ -27,22 +29,36 @@ protected void doFilterInternal(
filterChain.doFilter(request, response);
} catch (BusinessException e) {
String errorId = UUID.randomUUID().toString();
ErrorResponse responseBody = ErrorResponse.businessErrorResponseBuilder()
.errorId(errorId)
.error(e.error)
.build();

log.error(errorId, e);

response.setStatus(e.error.getHttpStatus());
response.setContentType("application/json; charset=UTF-8");

response.getWriter().write(
new ObjectMapper().writeValueAsString(
ErrorResponse.businessErrorResponseBuilder()
.errorId(errorId)
.error(e.error)
.build()
responseBody
)
);
} catch (RuntimeException e) {
String errorId = UUID.randomUUID().toString();
ErrorResponse responseBody = ErrorResponse.businessErrorResponseBuilder()
.errorId(errorId)
.error(GlobalError.INTERNAL_SERVER_ERROR)
.build();

log.error(errorId, e);

response.setStatus(500);
response.setContentType("application/json; charset=UTF-8");

response.getWriter().write(
new ObjectMapper().writeValueAsString(responseBody)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public record SignUpRequest(

@NotNull
@NotNull(message = "닉네임은 필수 입력값입니다.")
String nickname
) {

Expand Down

0 comments on commit c346b6d

Please sign in to comment.