You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Restful接口发送邮件时无法正常捕获错误问题咨询

问题原因

你配置的ConnectionRequest.setHandleErrorCodesInGlobalErrorHandler(false)仅禁用了全局错误处理器对错误响应码的统一处理,但你使用的Rest请求默认内置了失败回调逻辑,服务端返回500这类错误码时会自动触发系统报错弹窗,且这类服务端返回的错误默认不会进入你写的catch块,导致你无法按自定义逻辑处理。

解决方案

在Rest请求构造时添加failOnError(false)配置,禁用默认的错误抛出和弹窗行为,即可自行处理所有响应状态码,调整后的代码如下:

public static Response imprimePedido(Long pedidoId) {
    String serverPost = "";
    Response<String> r = null;
    if (Preferences.get("correo_prueba", null) != null) {
        serverPost = SERVER_URL + "blank_factura_impresion_pdf/blank_factura_impresion_pdf.php?pedidoId=" + Long.toString(pedidoId) + "&correo_prueba=" + Preferences.get("correo_prueba", null);
    } else {
        serverPost = SERVER_URL + "blank_factura_impresion_pdf/blank_factura_impresion_pdf.php?pedidoId=" + Long.toString(pedidoId);
    }
    try {
        r = Rest.post(serverPost)
                .header("token", UsuarioService.getToken())
                .jsonContent()
                // 新增本行,禁用默认错误弹窗
                .failOnError(false)
                .getAsString();
        if (r.getResponseCode() != 200) {
            System.out.println("No pude enviar correo");
            log("Error: " + r.getResponseErrorMessage());
            // 此处补充你的自定义错误处理逻辑
        }
    } catch (Exception e) {
        // 捕获网络中断、参数错误等本地执行阶段的异常
        e.printStackTrace();
        log("请求执行异常:" + e.getMessage());
    }
    return r;
}

如果需要全局禁用所有Rest请求的默认错误弹窗,可以在应用初始化阶段添加如下配置:

Rest.setDefaultFailOnError(false);

内容的提问来源于stack exchange,提问作者Rubén Villarreal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:06:03