Skip to content

Commit bd2b3be

Browse files
authored
release: 0.9.4 (#103)
2 parents 7f47ef5 + 0300593 commit bd2b3be

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)