This commit is contained in:
xds
2025-10-31 15:31:55 +03:00
parent 040da34ff7
commit 7972ea0fdf
117 changed files with 3691 additions and 2013 deletions

View File

@@ -1,109 +1,112 @@
package space.luminic.finance.api.exceptionHandlers
import jakarta.servlet.http.HttpServletRequest
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 {
data class ErrorResponse(
val timestamp: Long = System.currentTimeMillis(),
val status: Int,
val error: String,
val message: String?,
val path: String,
)
// Изменённый параметр request
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()
request: HttpServletRequest // <--- Изменённый тип
): ErrorResponse {
return ErrorResponse(
status = status.value(),
error = message,
message = e.message,
path = request.requestURI // <--- Получаем путь через HttpServletRequest
)
return errorResponse
}
@ExceptionHandler(AuthException::class)
// Изменённый параметр exchange
fun handleAuthenticationException(
ex: AuthException,
exchange: ServerWebExchange
): Mono<ResponseEntity<Map<String, Any?>>?> {
request: HttpServletRequest // <--- Изменённый тип
): ResponseEntity<ErrorResponse>? {
ex.printStackTrace()
return Mono.just(
ResponseEntity(
constructErrorBody(
ex,
ex.message.toString(),
HttpStatus.UNAUTHORIZED,
exchange.request
), HttpStatus.UNAUTHORIZED
)
return ResponseEntity(
constructErrorBody(
ex,
ex.message.toString(),
HttpStatus.UNAUTHORIZED,
request // <--- Передаём HttpServletRequest
),
HttpStatus.UNAUTHORIZED
)
}
@ExceptionHandler(NotFoundException::class)
// Изменённый параметр exchange
fun handleNotFoundException(
e: NotFoundException,
exchange: ServerWebExchange
): Mono<ResponseEntity<Map<String, Any?>>?> {
request: HttpServletRequest // <--- Изменённый тип
): ResponseEntity<ErrorResponse>? {
e.printStackTrace()
return Mono.just(
ResponseEntity(
constructErrorBody(
e,
e.message.toString(),
HttpStatus.NOT_FOUND,
exchange.request
), HttpStatus.NOT_FOUND
)
return ResponseEntity(
constructErrorBody(
e,
e.message.toString(),
HttpStatus.NOT_FOUND,
request // <--- Передаём HttpServletRequest
),
HttpStatus.NOT_FOUND
)
}
@ExceptionHandler(IllegalArgumentException::class)
// Изменённый параметр exchange
fun handleIllegalArgumentException(
e: IllegalArgumentException,
exchange: ServerWebExchange
): Mono<ResponseEntity<Map<String, Any?>>?> {
request: HttpServletRequest // <--- Изменённый тип
): ResponseEntity<ErrorResponse>? {
e.printStackTrace()
return Mono.just(
ResponseEntity(
constructErrorBody(
e,
e.message.toString(),
HttpStatus.BAD_REQUEST,
exchange.request
), HttpStatus.BAD_REQUEST
)
return ResponseEntity(
constructErrorBody(
e,
e.message.toString(),
HttpStatus.BAD_REQUEST,
request // <--- Передаём HttpServletRequest
),
HttpStatus.BAD_REQUEST
)
}
@ExceptionHandler(Exception::class)
// Изменённый параметр exchange
fun handleGenericException(
e: Exception,
exchange: ServerWebExchange
): Mono<out ResponseEntity<out Map<String, Any?>>?> {
request: HttpServletRequest // <--- Изменённый тип
): ResponseEntity<ErrorResponse>? {
e.printStackTrace()
return Mono.just(
ResponseEntity(
constructErrorBody(
e,
e.message.toString(),
HttpStatus.INTERNAL_SERVER_ERROR,
exchange.request
), HttpStatus.INTERNAL_SERVER_ERROR
)
return ResponseEntity(
constructErrorBody(
e,
e.message.toString(),
HttpStatus.INTERNAL_SERVER_ERROR,
request // <--- Передаём HttpServletRequest
),
HttpStatus.INTERNAL_SERVER_ERROR
)
}
}
}

View File

@@ -1,49 +1,37 @@
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))
}
}
//
//@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))
// }
//
//
//}