Skip to content

Commit 7123508

Browse files
authored
Merge pull request #4 from SpringSimpleUtils/dev
Added unsupported media type and method not allowed exception handlers.
2 parents 2999ceb + 4c65c1f commit 7123508

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group = "io.github.springsimpleutils"
13-
version = "0.1.0"
13+
version = "0.2.0"
1414
java.sourceCompatibility = JavaVersion.VERSION_11
1515

1616
repositories {

src/main/kotlin/ru/remmintan/simple/exceptions/RestExceptionHandler.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import org.springframework.http.ResponseEntity
77
import org.springframework.http.converter.HttpMessageNotReadableException
88
import org.springframework.validation.BindException
99
import org.springframework.validation.BindingResult
10+
import org.springframework.web.HttpMediaTypeNotSupportedException
11+
import org.springframework.web.HttpRequestMethodNotSupportedException
1012
import org.springframework.web.bind.MethodArgumentNotValidException
1113
import org.springframework.web.bind.annotation.ControllerAdvice
1214
import org.springframework.web.bind.annotation.ExceptionHandler
@@ -58,6 +60,29 @@ class RestExceptionHandler {
5860
fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException): ErrorDto
5961
= parseBindingResult(ex.bindingResult)
6062

63+
@ExceptionHandler(HttpRequestMethodNotSupportedException::class)
64+
fun handleMethodNotAllowedException(ex: HttpRequestMethodNotSupportedException): ResponseEntity<ErrorDto> {
65+
val allowedMethods = ex.supportedMethods?.toList()?.joinToString(", ") ?: ""
66+
val errorMessage = "Method ${ex.method} not allowed. Allowed methods: $allowedMethods."
67+
68+
val dto = ErrorDto(errorMessage)
69+
var responseEntity = ResponseEntity
70+
.status(HttpStatus.METHOD_NOT_ALLOWED);
71+
72+
val allowedHeaders = ex.supportedHttpMethods?.toTypedArray()
73+
if(allowedHeaders != null) {
74+
responseEntity = responseEntity.allow(*allowedHeaders)
75+
}
76+
77+
return responseEntity.body(dto)
78+
}
79+
80+
@ExceptionHandler(HttpMediaTypeNotSupportedException::class)
81+
@ResponseBody
82+
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
83+
fun handleUnsupportedMediaTypeException(ex: HttpMediaTypeNotSupportedException): ErrorDto =
84+
ErrorDto(ex.localizedMessage)
85+
6186
private fun parseBindingResult(br: BindingResult): ErrorDto {
6287
val errors = br.allErrors.map {it.defaultMessage}.joinToString("\n")
6388
return ErrorDto(errors)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ru.remmintan.simple.exceptions
2+
3+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.boot.test.context.SpringBootTest
8+
import org.springframework.boot.test.web.client.TestRestTemplate
9+
import org.springframework.http.*
10+
import ru.remmintan.simple.exceptions.dtos.ErrorDto
11+
import ru.remmintan.simple.exceptions.testapp.TestApplication
12+
13+
@SpringBootTest(classes = [TestApplication::class], webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
14+
class HttpProtocolExceptionsTest {
15+
16+
@Autowired
17+
private lateinit var rest: TestRestTemplate
18+
19+
private val mapper = jacksonObjectMapper()
20+
21+
@Test
22+
fun methodNotAllowedTest() {
23+
val message = "Method POST not allowed. Allowed methods: GET."
24+
25+
val result = rest.postForEntity("/test/badrequest", null, String::class.java)
26+
assertThat(result.statusCode).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED)
27+
assertThat(result.hasBody()).isTrue
28+
val error = mapper.readValue(result.body, ErrorDto::class.java)
29+
assertThat(error.message).isEqualTo(message)
30+
}
31+
32+
@Test
33+
fun notFoundTest() {
34+
val result = rest.getForEntity("/path/that/doesnt/exist", String::class.java)
35+
assertThat(result.statusCode).isEqualTo(HttpStatus.NOT_FOUND)
36+
}
37+
38+
@Test
39+
fun mediaTypeNotSupportedTest() {
40+
val invalidHeaders = HttpHeaders()
41+
invalidHeaders.contentType = MediaType.TEXT_PLAIN
42+
val invalidEntity = HttpEntity("", invalidHeaders);
43+
44+
val errorMessage = "Content type 'text/plain;charset=UTF-8' not supported"
45+
46+
val result = rest.postForEntity("/validation/mapping", invalidEntity, String::class.java)
47+
assertThat(result.statusCode).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
48+
assertThat(result.hasBody()).isTrue
49+
val error = mapper.readValue(result.body, ErrorDto::class.java)
50+
assertThat(error.message).isEqualTo(errorMessage)
51+
}
52+
53+
}

0 commit comments

Comments
 (0)