【问题标题】:Differentiating Types of API (Apex) Exceptions In JavaScript Try/Catch区分 JavaScript Try/Catch 中的 API (Apex) 异常类型
【发布时间】:2024-01-19 18:33:01
【问题描述】:

Lightning Web Component 中,我正在对 Apex 方法进行 API 调用,该方法可能引发异常。目前,在 catch 块中,有代码检查异常中的特定字符串,b/c 如果它是某种类型的异常,我们想向用户显示某种消息;否则我们想显示另一个通用消息。显然通过字符串来确定具体的异常是不可靠的。

JavaScript 中是否有一种好方法可以确定引发了哪个特定 Apex 异常?

我注意到当前传递给 catch 块的错误对象确实包含 headers 属性,我想知道是否可以在其中传递一个自定义值,例如 exceptionType?比如:

async myFunc() {
    try {
        const response = await myApexMethodCall();
    } catch(err) {
        if (err.headers.exceptionType === 'E123') {
            alert('The order must be associated with a case before processing');
        } else {
            alert('There was an error with the API call. Please contact your administrator');
        }
    }
}

【问题讨论】:

  • 去掉java标签,java和javascript不是一回事。
  • @goatofanerd - 我添加的 Java 标签不是 JavaScript 的 b/c,而是 Apex 的 b/c,它基本上是 Java 的扩展。
  • @user16320675 - 我需要在 JS 端进行测试。

标签: javascript java try-catch apex lwc


【解决方案1】:

您可以创建一个代表响应的自定义类。例如,

public with sharing class CustomOrderStatusResponse {

    @AuraEnabled
    public String errorCode { get; set; }
    @AuraEnabled
    public Map<String, String> result { get; set; }

    public CustomOrderStatusResponse(String errorCode, Map<String, String> result) {
        this.errorCode = errorCode;
        this.result = result;
    }
}

然后,在控制器类中,您可以根据不同的场景创建CustomOrderStatusResponse 的实例,并可以为errorCode 设置不同的值。例如,

public with sharing class CustomOrderStatus {
    @AuraEnabled
    public static CustomOrderStatusResponse getOrderStatus(String orderId){
        CustomOrderStatusResponse response;
        try {
            if (orderId.equals('123')) {
                Map<String, String> result = new Map<String, String>();
                result.put('status', 'completed');
                response = new CustomOrderStatusResponse(null, result);
            } else if (orderId.equals('456')) {
                response = new CustomOrderStatusResponse('E123', null);
            } else if (orderId.equals('789')) {
                response = new CustomOrderStatusResponse('E789', null);
            }
            return response;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}

在前端 (LWC) 中,您可以执行以下操作:

import { LightningElement } from 'lwc';
import getOrderStatus from "@salesforce/apex/CustomOrderStatus.getOrderStatus";

export default class OrderStatus extends LightningElement {
    handleFirstClick(event) {
        this.displayOrderStatus('123');
    }

    handleSecondClick(event) {
        this.displayOrderStatus('456');
    }

    handleThirdClick(event) {
        this.displayOrderStatus('789');
    }

    displayOrderStatus(orderId) {
        getOrderStatus({ orderId: orderId })
            .then(response => {
                if (response.errorCode === 'E123') {
                    alert('The order must be associated with a case before processing');
                } else if (response.errorCode) {
                    alert('There was an error with API Call');
                } else {
                    alert('Status: ' + response.result.status);
                }
            })
            .catch(console.error);
    }
}

参考:https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_wire_methodhttps://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_call_imperative

我希望这能回答您的问题。欢迎任何建议和cmets。

【讨论】:

    最近更新 更多