-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…exceptions gh-2: adds exception handling example
- Loading branch information
Showing
8 changed files
with
163 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The provided password doesn't comply with the minimum security requirements... |
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 @@ | ||
The request content is not valid. |
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 @@ | ||
The user being created already exists. |
1 change: 1 addition & 0 deletions
1
code/tic-tac-tow-service/docs/problems/user-or-password-are-invalid
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 @@ | ||
The provided username or password are invalid. |
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
48 changes: 48 additions & 0 deletions
48
...tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/pipeline/CustomExceptionHandler.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,48 @@ | ||
package pt.isel.daw.tictactow.http.pipeline | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.http.converter.HttpMessageNotReadableException | ||
import org.springframework.validation.BindException | ||
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.servlet.mvc.method.annotation.ResponseEntityExceptionHandler | ||
import pt.isel.daw.tictactow.http.model.Problem | ||
|
||
@ControllerAdvice | ||
class CustomExceptionHandler : ResponseEntityExceptionHandler() { | ||
|
||
override fun handleBindException( | ||
ex: BindException, | ||
headers: HttpHeaders, | ||
status: HttpStatus, | ||
request: WebRequest | ||
): ResponseEntity<Any> { | ||
log.info("Handling BindException: {}", ex.message) | ||
return Problem.response(400, Problem.invalidRequestContent) | ||
} | ||
|
||
override fun handleHttpMessageNotReadable( | ||
ex: HttpMessageNotReadableException, | ||
headers: HttpHeaders, | ||
status: HttpStatus, | ||
request: WebRequest | ||
): ResponseEntity<Any> { | ||
log.info("Handling HttpMessageNotReadableException: {}", ex.message) | ||
return Problem.response(400, Problem.invalidRequestContent) | ||
} | ||
|
||
@ExceptionHandler( | ||
Exception::class, | ||
) | ||
fun handleAll(): ResponseEntity<Unit> { | ||
return ResponseEntity.status(500).build() | ||
} | ||
|
||
companion object { | ||
private val log = LoggerFactory.getLogger(CustomExceptionHandler::class.java) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
code/tic-tac-tow-service/src/test/kotlin/pt/isel/daw/tictactow/http/InternalErrorTests.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,55 @@ | ||
package pt.isel.daw.tictactow.http | ||
|
||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.jdbi.v3.core.Jdbi | ||
import org.junit.jupiter.api.Test | ||
import org.postgresql.ds.PGSimpleDataSource | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.TestConfiguration | ||
import org.springframework.boot.test.web.server.LocalServerPort | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Primary | ||
import org.springframework.test.web.reactive.server.WebTestClient | ||
import pt.isel.daw.tictactow.repository.jdbi.configure | ||
import java.util.* | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class InternalErrorTests { | ||
|
||
@TestConfiguration | ||
class TestConfig { | ||
@Bean | ||
@Primary | ||
fun testJdbi() = Jdbi.create( | ||
PGSimpleDataSource().apply { | ||
setURL("jdbc:postgresql://bad-host:5432/db?user=dbuser&password=changeit") | ||
} | ||
).configure() | ||
} | ||
|
||
@LocalServerPort | ||
var port: Int = 0 | ||
|
||
@Test | ||
fun `Unknown exceptions are mapped into a 500 without a response content`() { | ||
// given: an HTTP client | ||
val client = WebTestClient.bindToServer().baseUrl("http://localhost:$port").build() | ||
|
||
// and: a random user | ||
val username = UUID.randomUUID().toString() | ||
val password = "is-this-strong?" | ||
|
||
// when: creating a user | ||
// then: the response is a 400 with the proper type | ||
client.post().uri("/users") | ||
.bodyValue( | ||
mapOf( | ||
"username" to username, | ||
"password" to password | ||
) | ||
) | ||
.exchange() | ||
.expectStatus().value(equalTo(500)) | ||
.expectHeader().doesNotExist("Content-Type") | ||
} | ||
} |
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