【问题标题】:Spring Webflux WebExceptionHandler not being Triggered by OptimisticLockingFailureSpring Webflux WebExceptionHandler 未被 OptimisticLockingFailure 触发
【发布时间】:2020-01-10 07:37:08
【问题描述】:

我有一个 GlobalExceptionHandler 来捕获所有抛出的异常并返回正确的返回码。 除非发生 OptimisticLockingFailureException 并且我不知道为什么会这样,否则一切正常。

全局异常处理程序:

@Component
@Order(-2)
class GlobalExceptionHandler : WebExceptionHandler {

    override fun handle(exchange: ServerWebExchange, throwable: Throwable): Mono<Void> = handleException(throwable)
            .flatMap { it.writeTo(exchange, HandlerStrategiesResponseContext(HandlerStrategies.withDefaults())) }
            .flatMap { Mono.empty<Void>() }

    private fun handleException(ex: Throwable): Mono<ServerResponse> = when (ex) {
        is EntityNotFoundException -> notFound(ex.message)
        is InvalidRequestException,
        is ReferenceKeyNotKnown,
        is InvalidContractTypeException -> badRequest(ex.message)
        is DuplicateKeyException,
        is TransactionException,
        is OptimisticLockingFailureException -> conflict(ex.message)
        else -> errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.message)
    }

    private class HandlerStrategiesResponseContext(val strategies: HandlerStrategies) : ServerResponse.Context {
        override fun viewResolvers(): MutableList<ViewResolver> = strategies.viewResolvers()
        override fun messageWriters(): MutableList<HttpMessageWriter<*>> = strategies.messageWriters()
    }

}

notFound、badRequest 和 errorResponse 函数返回带有正确状态码和消息的 Mono。

谁能帮助我并指出正确的方向,为什么 OptimistickLockingException 不会触发此问题?

谢谢!

【问题讨论】:

    标签: spring-boot kotlin spring-data-mongodb spring-webflux


    【解决方案1】:

    所以异常处理程序没有被触发的原因是我们的处理程序中仍然有一个 .onErrorContinue 步骤,它记录了异常而没有其他内容。将日志记录移至 GlobalExceptionHandler 并删除 onErrorContinue 后,GlobalExceptionHandler 工作正常

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2018-05-17
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2020-05-01
      相关资源
      最近更新 更多