通过switch / case语句显示错误消息

时间:2018-02-23 00:04:08

标签: javascript function error-handling switch-statement

我正在尝试让我的错误模式根据网络响应显示正确的错误消息。网络响应如下所示:

error = {
            'type': 'https://stellar.org/horizon-errors/transaction_failed',
            'title': 'Transaction Failed',
            'status': 400,
            'detail': 'The transaction failed when submitted to the stellar network. The `extras.result_codes` field on this response contains further details.  Descriptions of each code can be found at: https://www.stellar.org/developers/learn/concepts/list-of-operations.html',
            'instance': 'horizon-001a/TUy69KAizJ-79400604',
            'extras': {
                'envelope_xdr': 'AAAAAKNx5adLnZvZNZti9rBp6vqErRd+dzSiyScEf0UxP8vhAAABLAD6kFwAAAACAAAAAAAAAAEAAAAMb2ZmZXJfdmlhX3NwAAAAAwAAAAAAAAAGAAAAAU1PQkkAAAAAPHEwK55l2uMBECcQxzsOUsAWg1YwXwD+ZUTDWZEbZWR////////82AAAAAAAAAADAAAAAAAAAAFNT0JJAAAAADxxMCueZdrjARAnEMc7DlLAFoNWMF8A/mVEw1mRG2VkAAAAAADkidwA9r8LAJiWgAAAAAAAAAAAAAAAAAAAAAEAAAAADMgUTSrxhwESnrGfKRf+Rf+kP7rKJ12mh8Ig10EpSFEAAAAAAAAAAAAAV+QAAAAAAAAAATE/y+EAAABAA0/SoAyidZrQs0OWRdISsj9D/XodktuATBCUSntxC2Ke1x6d0I0hOnR3PRm3pnysnF2BAF9YzJfhltPiWxNaDg==',
                'result_codes': {
                    'transaction': 'tx_failed',
                    'operations': [
                        'op_success',
                        'op_low_reserve',
                        'op_success'
                    ]
                },
                'result_xdr': 'AAAAAAAAASz/////AAAAAwAAAAAAAAAGAAAAAAAAAAAAAAAD////9AAAAAAAAAABAAAAAAAAAAA='
            }
        };

错误消息基于网络返回的JSON对象中的操作代码(例如,op_low_reserve)。由于我作为开发人员的经验不足,我无法弄清楚如何正确 1)阅读每个错误代码。 2)找到相应的错误消息(不中断或返回)。 3)将该错误消息发送回errorMessage函数。 4)打印出每条错误信息。

目前,每条错误消息都会被发送回errorMessage函数(我不知道为什么,现在只有其中一个案例正在完成)并且正在用逗号打印出来(来自数组) 。我不确定我当前的方向,检查所有情况并推送错误消息,如果情况=== true,然后发送回数组,如果一个好的方法来解决这个问题。我很欣赏任何想法和方向。谢谢!

export class ErrorModal {
    get errorMessage() {
        return 'There was an error in submitting the transaction to the network. ' +
            'The transaction failed for the following reason(s):<br><br><ul>' +
            this.isRelevantToUser(this.error) +

            '</ul>';
    }

isRelevantToUser(errors) {
    // Iterate through error codes and return error messages.
    for (var i = 0; i < errors.extras.result_codes.operations.length; i++) {
        let errorsArr = [];
        // There are no breaks in switch/case block due to multiple possible errors.
        switch (errors.extras.result_codes.operations[i]) {
            // op_success case is FOR TESTING
            case "op_success":
                errorsArr.push('<li>' + ((i + 1) + ") The transaction submitted succesfully.") + '</li>');
            case "op_underfunded":
                errorsArr.push('<li>' + ((i + 1) + ") Your account does not have enough funds.") + '</li>');
            case "op_low_reserve":
                errorsArr.push('<li>' + ((i + 1) + ") Your account's reserve balance is too low for this transaction to process.") + '</li>');
            case "op_line_full":
                errorsArr.push('<li>' + ((i + 1) + ") This transaction will exceed destination account's trust limit for the asset being sent.") + '</li>');
            case "op_no_issuer":
                errorsArr.push('<li>' + ((i + 1) + ") Issuing asset not correctly specified.") + '</li>');
            case "op_underfunded":
                errorsArr.push('<li>' + ((i + 1) + ") Your account does not have enough funds.") + '</li>');

            return errorsArr;
        }
    }
    return true;
}

Error Modal Image

Error Modal after adding "continue" to case statements.

0 个答案:

没有答案