1
1
package co.bearus.magcloud.config
2
2
3
+ import co.bearus.magcloud.controller.dto.response.ErrorResponse
3
4
import co.bearus.magcloud.domain.exception.DomainException
4
- import co.bearus.magcloud.domain.exception.NotFoundException
5
- import co.bearus.magcloud.domain.exception.UnauthorizedException
5
+ import co.bearus.magcloud.domain.exception.ErrorCode
6
+ import co.bearus.magcloud.domain.exception.UnAuthenticatedException
7
+ import co.bearus.magcloud.domain.exception.UnAuthorizedException
8
+ import co.bearus.magcloud.domain.type.ContextLanguage
9
+ import jakarta.servlet.http.HttpServletRequest
6
10
import org.springframework.http.ResponseEntity
7
11
import org.springframework.web.bind.MethodArgumentNotValidException
8
12
import org.springframework.web.bind.annotation.ExceptionHandler
@@ -11,36 +15,70 @@ import org.springframework.web.bind.annotation.RestControllerAdvice
11
15
@RestControllerAdvice
12
16
class ControllerAdvice {
13
17
@ExceptionHandler
14
- fun userExHandle (e : NotFoundException ): ResponseEntity <co.bearus.magcloud.controller.dto.response.APIResponse >? {
15
- return ResponseEntity .notFound().build()
18
+ fun validationException (
19
+ exception : MethodArgumentNotValidException ,
20
+ request : HttpServletRequest ,
21
+ ): ResponseEntity <ErrorResponse > {
22
+ return ResponseEntity
23
+ .badRequest()
24
+ .body(
25
+ errorBody(ErrorCode .VALIDATION_EXCEPTION , request.getLanguage())
26
+ )
16
27
}
17
28
18
- @ExceptionHandler
19
- fun validationException (e : MethodArgumentNotValidException ): ResponseEntity <co.bearus.magcloud.controller.dto.response.APIResponse >? {
20
- return ResponseEntity .badRequest()
29
+ @ExceptionHandler(value = [UnAuthenticatedException ::class ])
30
+ fun handleUnAuthenticated (
31
+ exception : UnAuthenticatedException ,
32
+ request : HttpServletRequest ,
33
+ ): ResponseEntity <ErrorResponse > {
34
+ return ResponseEntity
35
+ .status(401 )
21
36
.body(
22
- co.bearus.magcloud.controller.dto.response.APIResponse .error(
23
- e.fieldErrors.firstOrNull()?.defaultMessage ? : " 알 수 없는 오류입니다"
24
- )
37
+ errorBody(exception.errorCode, request.getLanguage())
25
38
)
26
39
}
27
40
28
- @ExceptionHandler
29
- fun unauthorized (e : UnauthorizedException ): ResponseEntity <co.bearus.magcloud.controller.dto.response.APIResponse >? {
30
- return ResponseEntity .status(401 )
31
- .body(co.bearus.magcloud.controller.dto.response.APIResponse .error(" 토큰이 만료되었거나 사용할 수 없습니다" ))
41
+ @ExceptionHandler(value = [UnAuthorizedException ::class ])
42
+ fun handleUnAuthorized (
43
+ exception : UnAuthorizedException ,
44
+ request : HttpServletRequest ,
45
+ ): ResponseEntity <ErrorResponse > {
46
+ return ResponseEntity
47
+ .status(403 )
48
+ .body(
49
+ errorBody(exception.errorCode, request.getLanguage())
50
+ )
32
51
}
33
52
34
- @ExceptionHandler
35
- fun userExHandle (e : DomainException ): ResponseEntity <co.bearus.magcloud.controller.dto.response.APIResponse >? {
36
- return ResponseEntity .badRequest()
37
- .body(co.bearus.magcloud.controller.dto.response.APIResponse .error(e.message!! ))
53
+ @ExceptionHandler(value = [DomainException ::class ])
54
+ fun handleDomainException (
55
+ exception : DomainException ,
56
+ request : HttpServletRequest ,
57
+ ): ResponseEntity <ErrorResponse > {
58
+ return ResponseEntity
59
+ .badRequest()
60
+ .body(
61
+ errorBody(exception.errorCode, request.getLanguage())
62
+ )
38
63
}
39
64
40
65
@ExceptionHandler
41
- fun defaultHandle (e : RuntimeException ): ResponseEntity <co.bearus.magcloud.controller.dto.response.APIResponse >? {
42
- e.printStackTrace()
43
- return ResponseEntity .badRequest()
44
- .body(co.bearus.magcloud.controller.dto.response.APIResponse .error(" 알 수 없는 오류가 발생했습니다" ))
66
+ fun unhandledException (
67
+ exception : Exception ,
68
+ request : HttpServletRequest ,
69
+ ): ResponseEntity <ErrorResponse > {
70
+ exception.printStackTrace()
71
+ return ResponseEntity
72
+ .badRequest()
73
+ .body(
74
+ errorBody(ErrorCode .UNKNOWN_EXCEPTION , request.getLanguage())
75
+ )
45
76
}
77
+
78
+ private fun errorBody (errorCode : ErrorCode , contextLanguage : ContextLanguage ) = ErrorResponse (
79
+ code = errorCode.code,
80
+ message = errorCode.message[contextLanguage] ? : errorCode.code,
81
+ )
82
+
83
+ private fun HttpServletRequest.getLanguage () = this .getAttribute(" language" ) as ContextLanguage
46
84
}
0 commit comments