-
-
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.
Adds ExceptionHandler Autoconfiguration
- Loading branch information
1 parent
23f6f65
commit a230c1f
Showing
14 changed files
with
220 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
id("org.springframework.boot").version("2.4.2") | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(group = "org.springframework.boot", name = "spring-boot-starter-web") | ||
implementation(project(":spring-json-api-starter")) | ||
} |
13 changes: 13 additions & 0 deletions
13
sample/src/main/kotlin/io/hndrs/api/sample/SampleApplication.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,13 @@ | ||
package io.hndrs.api.sample | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
|
||
@SpringBootApplication | ||
open class SampleApplication { | ||
|
||
} | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<SampleApplication>(*args) | ||
} |
16 changes: 16 additions & 0 deletions
16
sample/src/main/kotlin/io/hndrs/api/sample/SampleController.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,16 @@ | ||
package io.hndrs.api.sample | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.server.ResponseStatusException | ||
|
||
@RestController | ||
class SampleController { | ||
|
||
|
||
@GetMapping("/test-errors") | ||
fun errors() { | ||
throw ResponseStatusException(HttpStatus.CONFLICT) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
dependencies { | ||
implementation(project(":spring-json-api")) | ||
testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test") | ||
} |
14 changes: 14 additions & 0 deletions
14
...g-json-api-starter/src/main/kotlin/io/hndrs/api/autoconfigure/JsonApiAutoConfiguration.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,14 @@ | ||
package io.hndrs.api.autoconfigure | ||
|
||
import io.hndrs.api.exception.ExceptionHandler | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
open class JsonApiAutoConfiguration { | ||
|
||
@Bean | ||
open fun exceptionAdvice(): ExceptionHandler { | ||
return ExceptionHandler() | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
spring-json-api-starter/src/main/resources/META-INF/spring.factories
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,2 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
io.hndrs.api.autoconfigure.JsonApiAutoConfiguration |
22 changes: 22 additions & 0 deletions
22
...on-api-starter/src/test/kotlin/io/hndrs/api/autoconfigure/JsonApiAutoConfigurationTest.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,22 @@ | ||
package io.hndrs.api.autoconfigure | ||
|
||
import io.hndrs.api.exception.ExceptionHandler | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.autoconfigure.AutoConfigurations | ||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner | ||
|
||
@DisplayName("JsonApi Autoconfiguration") | ||
internal class JsonApiAutoConfigurationTest { | ||
|
||
@Test | ||
fun test() { | ||
WebApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of(JsonApiAutoConfiguration::class.java) | ||
).run { | ||
Assertions.assertNotNull(it.getBean(ExceptionHandler::class.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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dependencies { | ||
api(group = "org.springframework.boot", name = "spring-boot-starter-web") | ||
} |
43 changes: 43 additions & 0 deletions
43
spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.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,43 @@ | ||
package io.hndrs.api.exception | ||
|
||
import io.hndrs.api.response.ErrorResponse | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.context.request.WebRequest | ||
import org.springframework.web.server.ResponseStatusException | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler | ||
|
||
@ControllerAdvice | ||
class ExceptionHandler : ResponseEntityExceptionHandler() { | ||
|
||
@ExceptionHandler(ResponseStatusException::class) | ||
fun handleResponseStatusExceptions(ex: ResponseStatusException, request: WebRequest): ResponseEntity<Any> { | ||
return ResponseEntity.status(ex.status).body(ErrorResponse(ex.status, ex.reason)) | ||
} | ||
|
||
@ExceptionHandler(Exception::class) | ||
fun handleExceptions(ex: Exception, request: WebRequest): ResponseEntity<Any> { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body(ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.message)) | ||
} | ||
|
||
override fun handleExceptionInternal( | ||
ex: Exception, | ||
body: Any?, | ||
headers: HttpHeaders, | ||
status: HttpStatus, | ||
request: WebRequest | ||
): ResponseEntity<Any> { | ||
return ResponseEntity.status(status).body(ErrorResponse(status, ex.message)) | ||
} | ||
|
||
companion object { | ||
private val LOG: Logger = LoggerFactory.getLogger(ExceptionHandler::class.java) | ||
} | ||
} | ||
|
40 changes: 40 additions & 0 deletions
40
spring-json-api/src/main/kotlin/io/hndrs/api/response/ErrorResponse.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,40 @@ | ||
package io.hndrs.api.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import org.springframework.http.HttpStatus | ||
|
||
|
||
data class ErrorResponse( | ||
@field:JsonProperty("errors") val errors: List<Error> | ||
) { | ||
|
||
/** | ||
* Convenience constructor to create response for one error | ||
*/ | ||
constructor(error: Error) : this(listOf(error)) | ||
|
||
/** | ||
* Convenience constructor to create a simple response based on the http status | ||
*/ | ||
constructor(httpStatus: HttpStatus, detail: String? = null) : this( | ||
Error( | ||
httpStatus.value().toString(), | ||
httpStatus.reasonPhrase, | ||
detail | ||
) | ||
) | ||
|
||
data class Error( | ||
@field:JsonProperty("status") val status: String, | ||
@field:JsonProperty("title") val title: String, | ||
@field:JsonProperty("detail") val detail: String? = null | ||
) | ||
|
||
|
||
/** | ||
* Empty companion object to allow extensions | ||
*/ | ||
companion object | ||
|
||
} | ||
|
19 changes: 19 additions & 0 deletions
19
spring-json-api/src/main/kotlin/io/hndrs/api/response/Meta.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,19 @@ | ||
package io.hndrs.api.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class ListMeta( | ||
|
||
@field:JsonProperty("offset") val offset: Int, | ||
|
||
@field:JsonProperty("limit") val limit: Int, | ||
|
||
@field:JsonProperty("isLast") val isLast: Boolean, | ||
|
||
@field:JsonProperty("totalElements") val totalElements: Int, | ||
) { | ||
/** | ||
* Empty companion object to allow extensions | ||
*/ | ||
companion object | ||
} |
11 changes: 11 additions & 0 deletions
11
spring-json-api/src/main/kotlin/io/hndrs/api/response/Response.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,11 @@ | ||
package io.hndrs.api.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class Response<T>( | ||
|
||
@field:JsonProperty("data") val data: T, | ||
|
||
@field:JsonProperty("meta") val meta: Any? = null | ||
) | ||
|