【问题标题】:Handling errors using Parse.com Cloud Code and javascript API使用 Parse.com 云代码和 javascript API 处理错误
【发布时间】:2013-12-31 19:33:55
【问题描述】:

使用 Parse.com 云代码处理错误的最佳方法是什么。我可以使用 console.log 和 Firebug 来查看 Parse Cloud Code 何时引发错误,但我需要一些帮助来通知客户端出现问题。双方的一些示例代码真的很棒——云代码和客户端javascript代码。

【问题讨论】:

    标签: parse-platform


    【解决方案1】:

    我更喜欢这种方式-

    在 Cloud Code 上制作一个 ErrorHandler.JS 文件 -

    exports.sendError = function(response, message, data) {
        console.log("Message - " +  message + " Data - " + JSON.stringify(data)); // To print LOG on Cloud Code
        // Moreover you can use any of - "console.error/warn" - as mentioned - https://parse.com/docs/cloud_code_guide#logging
        response.error({
            status      : false, // Indicates EXECUTION STATUS - I am using "successHandler" also & using STATUS as "true" 
            message     : message, // Refers to Error Message
            data        : data || {} // Error Object or your customized Object
        });
    }
    

    & 客户端您可以根据需要打印所有数据,或者您可以只向用户显示警报消息。

    为了开发目的,最好同时检查 SERVER 端和 CLIENT 端 LOG,因为 PARSE Cloud Code 仅在 LOG 中存储最后 100 条消息

    & 为了实现正确的 LOGGING,您必须根据 CLASS 制作一些具有正确存储结构的自定义过程。

    【讨论】:

      【解决方案2】:

      解析有a section on Error Handling for Promises

      例如在 Cloud Code 中运行查询时

      query.find().then(function(result){ ... }, 
         function(error){
             response.error("Error occurred: " + error.message);
         }
      

      这将向客户端发送错误消息。

      【讨论】:

        【解决方案3】:

        作为一个实验,我尝试response.error 使用各种字符串/对象,下面是每个返回的内容(注释显示了返回给客户端的值)。

        本质上,它总是返回代码 141,而你只能返回一个字符串。我很惊讶从异常中传递众所周知的err 对象返回{} 我猜这是出于安全原因。我不明白为什么你不能在服务器上console.log(err),因为这让我在试图弄清楚发生了什么时感到很困惑。你基本上总是需要在你的 console.log 语句中做err.message 来弄清楚到底发生了什么。

        response.error("Some String of text") // --> {code: 141, message: "Some String of text"}
        response.error( new Error("My Msg") ) // -->  {code: 141, message: "{}"}
        
        try {
            var x = asdf.blah;
        }catch(err) {
            return response.error(err.message); // -->  {code: 141, message: "asdf is not defined"}
        }
        
        response.error( err );  // -->  {code: 141, message: "{}"}
        response.error( Parse.Error(Parse.Error.VALIDATION_ERROR, "My Text") ); // --> {code: 141, message: "An error has occurred"}
        

        【讨论】:

        • 现在,如果您调用 response.error(code, message),您可以返回与 141 不同的代码。
        猜你喜欢
        • 2015-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多