如何使用微配置文件记录内部错误

问题描述

假设以下代码是用 Quarkus 编写的。但也可以和 micronaut 一起使用。

@POST
@Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @APIResponses(
            value = {
                    @APIResponse(
                            responseCode = "201",description = "Customer Created"),@APIResponse(
                            responseCode = "400",description = "Customer already exists for customerId")
            }
    )
    public Response post(@Valid Customer customer) {
        final Customer saved = customerService.save(customer);
        return Response.status(Response.Status.CREATED).entity(saved).build();
    }

客户定义包括一个字段pictureUrl。 CustomerService 负责验证 URL 是有效的 URL 并且图像确实存在。

这意味着服务将处理以下异常:MalformedURLException 和 IOException。 CustomerService 捕获这些错误并抛出特定于应用程序的异常以报告图像不存在或路径不正确:ApplicationException。

您如何使用微配置文件记录此错误案例?

我的研究表明我必须实现以下形式的异常映射器:

public class ApplicationExceptionMapper implements ExceptionMapper<NotFoundException> {

    @Override
    @APIResponse(responseCode = "404",description = "Image not Found",content = @Content(
            schema = @Schema(implementation = Customer.class)
        )
    )
    public Response toResponse(NotFoundException t) {
        return Response.status(404,t.getMessage()).build();
    }

}

一旦我有了这样的映射器,框架就会知道如何将我的异常转换为响应。我的分析正确吗?最佳做法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)