init
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package space.luminic.finance.api.exceptionHandlers
|
||||
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.server.ServerWebExchange
|
||||
import reactor.core.publisher.Mono
|
||||
import space.luminic.finance.configs.AuthException
|
||||
import space.luminic.finance.models.NotFoundException
|
||||
|
||||
@RestControllerAdvice
|
||||
class GlobalExceptionHandler {
|
||||
|
||||
fun constructErrorBody(
|
||||
e: Exception,
|
||||
message: String,
|
||||
status: HttpStatus,
|
||||
request: ServerHttpRequest
|
||||
): Map<String, Any?> {
|
||||
val errorResponse = mapOf(
|
||||
"timestamp" to System.currentTimeMillis(),
|
||||
"status" to status.value(),
|
||||
"error" to message,
|
||||
"message" to e.message,
|
||||
"path" to request.path.value()
|
||||
)
|
||||
return errorResponse
|
||||
}
|
||||
|
||||
@ExceptionHandler(AuthException::class)
|
||||
fun handleAuthenticationException(
|
||||
ex: AuthException,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<ResponseEntity<Map<String, Any?>>?> {
|
||||
ex.printStackTrace()
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
ex,
|
||||
ex.message.toString(),
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
exchange.request
|
||||
), HttpStatus.UNAUTHORIZED
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler(NotFoundException::class)
|
||||
fun handleNotFoundException(
|
||||
e: NotFoundException,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<ResponseEntity<Map<String, Any?>>?> {
|
||||
e.printStackTrace()
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.NOT_FOUND,
|
||||
exchange.request
|
||||
), HttpStatus.NOT_FOUND
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException::class)
|
||||
fun handleIllegalArgumentException(
|
||||
e: IllegalArgumentException,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<ResponseEntity<Map<String, Any?>>?> {
|
||||
e.printStackTrace()
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.BAD_REQUEST,
|
||||
exchange.request
|
||||
), HttpStatus.BAD_REQUEST
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception::class)
|
||||
fun handleGenericException(
|
||||
e: Exception,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<out ResponseEntity<out Map<String, Any?>>?> {
|
||||
e.printStackTrace()
|
||||
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
exchange.request
|
||||
), HttpStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package space.luminic.finance.api.exceptionHandlers
|
||||
import org.springframework.boot.autoconfigure.web.WebProperties
|
||||
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler
|
||||
import org.springframework.boot.web.error.ErrorAttributeOptions
|
||||
import org.springframework.boot.web.reactive.error.ErrorAttributes
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.core.annotation.Order
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.codec.ServerCodecConfigurer
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.reactive.function.BodyInserters
|
||||
import org.springframework.web.reactive.function.server.*
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@Component
|
||||
@Order(-2)
|
||||
class GlobalErrorWebExceptionHandler(
|
||||
errorAttributes: ErrorAttributes,
|
||||
applicationContext: ApplicationContext,
|
||||
serverCodecConfigurer: ServerCodecConfigurer
|
||||
) : AbstractErrorWebExceptionHandler(
|
||||
errorAttributes,
|
||||
WebProperties.Resources(),
|
||||
applicationContext
|
||||
) {
|
||||
|
||||
init {
|
||||
super.setMessageWriters(serverCodecConfigurer.writers)
|
||||
super.setMessageReaders(serverCodecConfigurer.readers)
|
||||
}
|
||||
|
||||
override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> {
|
||||
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse)
|
||||
}
|
||||
|
||||
private fun renderErrorResponse(request: ServerRequest): Mono<ServerResponse> {
|
||||
val errorAttributesMap = getErrorAttributes(
|
||||
request,
|
||||
ErrorAttributeOptions.of(
|
||||
ErrorAttributeOptions.Include.MESSAGE
|
||||
)
|
||||
)
|
||||
return ServerResponse.status(401)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(BodyInserters.fromValue(errorAttributesMap))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user