|
1 | 1 | package org.tobynguyen.solitar.exception |
2 | 2 |
|
3 | | -import jakarta.servlet.http.HttpServletRequest |
| 3 | +import org.springframework.http.HttpHeaders |
4 | 4 | import org.springframework.http.HttpStatus |
| 5 | +import org.springframework.http.HttpStatusCode |
| 6 | +import org.springframework.http.ProblemDetail |
5 | 7 | import org.springframework.http.ResponseEntity |
6 | 8 | import org.springframework.web.bind.MethodArgumentNotValidException |
7 | 9 | import org.springframework.web.bind.annotation.ExceptionHandler |
8 | | -import org.springframework.web.bind.annotation.ResponseStatus |
9 | 10 | import org.springframework.web.bind.annotation.RestControllerAdvice |
10 | | -import org.tobynguyen.solitar.model.dto.ErrorResponse |
| 11 | +import org.springframework.web.context.request.WebRequest |
| 12 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler |
11 | 13 |
|
12 | 14 | @RestControllerAdvice |
13 | | -class UrlExceptionHandler { |
14 | | - |
15 | | - @ExceptionHandler(MethodArgumentNotValidException::class) |
16 | | - @ResponseStatus(HttpStatus.BAD_REQUEST) |
17 | | - fun onValidationFailed(e: MethodArgumentNotValidException): ResponseEntity<Map<String, Any>> { |
18 | | - val map = buildMap { |
19 | | - e.bindingResult.fieldErrors.forEach { |
20 | | - put(it.field, it.defaultMessage ?: "Validation failed") |
| 15 | +class UrlExceptionHandler : ResponseEntityExceptionHandler() { |
| 16 | + |
| 17 | + override fun handleMethodArgumentNotValid( |
| 18 | + ex: MethodArgumentNotValidException, |
| 19 | + headers: HttpHeaders, |
| 20 | + status: HttpStatusCode, |
| 21 | + request: WebRequest, |
| 22 | + ): ResponseEntity<Any> { |
| 23 | + val invalidParams = |
| 24 | + ex.bindingResult.fieldErrors.map { |
| 25 | + mapOf("name" to it.field, "reason" to (it.defaultMessage ?: "Validation failed")) |
21 | 26 | } |
22 | | - } |
23 | 27 |
|
24 | | - return ResponseEntity.badRequest().body(map) |
| 28 | + val problemDetail = |
| 29 | + ProblemDetail.forStatusAndDetail( |
| 30 | + HttpStatus.BAD_REQUEST, |
| 31 | + "The request contained invalid data. Please check the 'invalid_params' array.", |
| 32 | + ) |
| 33 | + .apply { setProperty("invalid_params", invalidParams) } |
| 34 | + |
| 35 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail) |
25 | 36 | } |
26 | 37 |
|
27 | 38 | @ExceptionHandler(UrlNotFoundException::class) |
28 | | - @ResponseStatus(HttpStatus.NOT_FOUND) |
29 | | - fun onUrlNotFound(e: UrlNotFoundException, request: HttpServletRequest) = |
30 | | - ErrorResponse( |
31 | | - status = HttpStatus.NOT_FOUND.value(), |
32 | | - error = HttpStatus.NOT_FOUND.reasonPhrase, |
33 | | - message = e.message, |
34 | | - path = request.requestURI, |
35 | | - ) |
| 39 | + fun onUrlNotFound(e: UrlNotFoundException) = |
| 40 | + ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.message) |
36 | 41 |
|
37 | 42 | @ExceptionHandler(UrlExpiredException::class) |
38 | | - @ResponseStatus(HttpStatus.GONE) |
39 | | - fun onUrlExpired(e: UrlExpiredException, request: HttpServletRequest) = |
40 | | - ErrorResponse( |
41 | | - status = HttpStatus.GONE.value(), |
42 | | - error = HttpStatus.GONE.reasonPhrase, |
43 | | - message = e.message, |
44 | | - path = request.requestURI, |
45 | | - ) |
| 43 | + fun onUrlExpired(e: UrlExpiredException) = |
| 44 | + ProblemDetail.forStatusAndDetail(HttpStatus.GONE, e.message) |
46 | 45 |
|
47 | 46 | @ExceptionHandler(UrlDisabledException::class) |
48 | | - @ResponseStatus(HttpStatus.FORBIDDEN) |
49 | | - fun onUrlDisabled(e: UrlDisabledException, request: HttpServletRequest) = |
50 | | - ErrorResponse( |
51 | | - status = HttpStatus.FORBIDDEN.value(), |
52 | | - error = HttpStatus.FORBIDDEN.reasonPhrase, |
53 | | - message = e.message, |
54 | | - path = request.requestURI, |
55 | | - ) |
| 47 | + fun onUrlDisabled(e: UrlDisabledException) = |
| 48 | + ProblemDetail.forStatusAndDetail(HttpStatus.FORBIDDEN, e.message) |
56 | 49 |
|
57 | 50 | @ExceptionHandler(UrlShortCodeConflictedException::class) |
58 | | - @ResponseStatus(HttpStatus.CONFLICT) |
59 | | - fun onUrlShortCodeConflicted(e: UrlShortCodeConflictedException, request: HttpServletRequest) = |
60 | | - ErrorResponse( |
61 | | - status = HttpStatus.CONFLICT.value(), |
62 | | - error = HttpStatus.CONFLICT.reasonPhrase, |
63 | | - message = e.message, |
64 | | - path = request.requestURI, |
65 | | - ) |
| 51 | + fun onUrlShortCodeConflicted(e: UrlShortCodeConflictedException) = |
| 52 | + ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, e.message) |
66 | 53 | } |
0 commit comments