-
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.
feat: Create custom global error exception
- Loading branch information
Showing
2 changed files
with
35 additions
and
4 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
team-srv/src/main/kotlin/com/bouceka/exceptions/GlobalException.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,29 @@ | ||
package com.bouceka.exceptions | ||
|
||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.MutableHttpResponse | ||
import io.micronaut.http.annotation.Produces | ||
import io.micronaut.http.server.exceptions.ExceptionHandler | ||
import io.micronaut.http.server.exceptions.response.ErrorContext | ||
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor | ||
import jakarta.inject.Singleton | ||
|
||
class GlobalException(override val message: String?, val httpResponse: MutableHttpResponse<Any>) : RuntimeException() | ||
|
||
@Produces | ||
@Singleton | ||
@Requires(classes = [RuntimeException::class]) | ||
class GlobalExceptionHandler(private val errorResponseProcessor: ErrorResponseProcessor<Any>) : | ||
ExceptionHandler<GlobalException, HttpResponse<*>> { | ||
|
||
override fun handle(request: HttpRequest<*>, exception: GlobalException): HttpResponse<*> { | ||
return errorResponseProcessor.processResponse( | ||
ErrorContext.builder(request) | ||
.cause(exception) | ||
.errorMessage(exception.message) | ||
.build(), exception.httpResponse) // | ||
} | ||
} | ||
|
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
Spelling error. "Team" not found. :)