File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
src/main/kotlin/org/gitanimals/core Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ package org.gitanimals.core
2+
3+ data class ErrorResponse (
4+ val message : String ,
5+ ) {
6+
7+ companion object {
8+ fun from (exception : Exception ): ErrorResponse =
9+ ErrorResponse (exception.message ? : exception.localizedMessage)
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package org.gitanimals.core.advice
2+
3+ import org.gitanimals.core.ErrorResponse
4+ import org.springframework.http.HttpStatus
5+ import org.springframework.http.ResponseEntity
6+ import org.springframework.web.bind.MissingRequestHeaderException
7+ import org.springframework.web.bind.annotation.ExceptionHandler
8+ import org.springframework.web.bind.annotation.RestControllerAdvice
9+
10+ @RestControllerAdvice
11+ class GlobalExceptionHandler {
12+
13+ @ExceptionHandler(MissingRequestHeaderException ::class )
14+ fun handleMissingRequestHeaderException (exception : MissingRequestHeaderException ): ResponseEntity <ErrorResponse > {
15+ if (exception.headerName == " Authorization" ) {
16+ return ResponseEntity .status(HttpStatus .UNAUTHORIZED )
17+ .body(ErrorResponse .from(exception))
18+ }
19+
20+ return ResponseEntity .badRequest().body(ErrorResponse .from(exception))
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments