-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat : CustomExceptionHandlerFilter를 통한 예외처리
- Loading branch information
Showing
9 changed files
with
70 additions
and
12 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
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
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
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
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
48 changes: 48 additions & 0 deletions
48
src/main/java/com/prgrms/catchtable/security/filter/ExceptionHandlerFilter.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,48 @@ | ||
package com.prgrms.catchtable.security.filter; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.prgrms.catchtable.common.exception.ErrorResponse; | ||
import com.prgrms.catchtable.common.exception.custom.BadRequestCustomException; | ||
import com.prgrms.catchtable.common.exception.custom.NotFoundCustomException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.GenericFilter; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExceptionHandlerFilter extends GenericFilter { | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
|
||
HttpServletResponse httpServletResponse = (HttpServletResponse) response; | ||
|
||
try { | ||
chain.doFilter(request, response); | ||
} catch (BadRequestCustomException be) { | ||
httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
sendJson(httpServletResponse, toJson(new ErrorResponse(be.getErrorCode()))); | ||
} catch (NotFoundCustomException ne) { | ||
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
sendJson(httpServletResponse, toJson(new ErrorResponse(ne.getErrorCode()))); | ||
} | ||
} | ||
|
||
private String toJson(ErrorResponse response) throws JsonProcessingException { | ||
return mapper.writeValueAsString(response); | ||
} | ||
|
||
private void sendJson(HttpServletResponse response, String resultJson) throws IOException { | ||
response.setContentType("application/json;charset=UTF-8"); | ||
response.setContentLength(resultJson.getBytes().length); | ||
response.getWriter().write(resultJson); | ||
} | ||
} |
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
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
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