-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcb0893
commit 5693e9c
Showing
5 changed files
with
93 additions
and
9 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
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/ohayo/moyamoya/global/ErrorResponseSender.kt
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,39 @@ | ||
package com.ohayo.moyamoya.global | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import jakarta.servlet.http.HttpServletResponse | ||
import mu.KLogger | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Component | ||
import java.io.IOException | ||
|
||
@Component | ||
class ErrorResponseSender( | ||
private val objectMapper: ObjectMapper, | ||
private val logger: KLogger | ||
) { | ||
|
||
fun send(response: HttpServletResponse, customException: CustomException) = | ||
send(response, customException.status, customException.message) | ||
|
||
fun send(response: HttpServletResponse, status: HttpStatus, message: String? = null) { | ||
try { | ||
response.apply { | ||
this.status = status.value() | ||
contentType = MediaType.APPLICATION_JSON_VALUE | ||
characterEncoding = "UTF-8" | ||
} | ||
response.writer.write( | ||
objectMapper.writeValueAsString( | ||
ErrorRes( | ||
status = status.value(), | ||
message = message ?: status.reasonPhrase | ||
) | ||
) | ||
) | ||
} catch (e: IOException) { | ||
logger.error("ErrorResponseSender.send", e) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/ohayo/moyamoya/global/HttpExceptionFilter.kt
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,29 @@ | ||
package com.ohayo.moyamoya.global | ||
|
||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.converter.HttpMessageNotReadableException | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.HttpRequestMethodNotSupportedException | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
@Component | ||
class HttpExceptionFilter( | ||
private val sender: ErrorResponseSender | ||
): OncePerRequestFilter() { | ||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
try { | ||
filterChain.doFilter(request, response) | ||
} catch (e: HttpRequestMethodNotSupportedException) { | ||
sender.send(response = response, status = HttpStatus.METHOD_NOT_ALLOWED) | ||
} catch (e: HttpMessageNotReadableException) { | ||
sender.send(response = response, status = HttpStatus.BAD_REQUEST) | ||
} | ||
} | ||
} |
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