node.js - 在云函数中处理 HTTP 请求时读取 ECONNRESET 错误

标签 node.js firebase google-cloud-functions app-store httprequest

我正在尝试向 Firebase 云函数中的 App Store verifyReceipt 端点发送发布请求。但是,我在云函数日志中收到以下错误:

{ Error: read ECONNRESET
    at TLSWrap.onread (net.js:622:25)
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read',

但是,此错误只是偶尔发生。它不会每次都会发生,所以该功能显然可以工作,但有时会出现问题,我不确定是什么。

与此相关的大多数其他解决方案都是由于 promise 错误造成的,但我不认为这是这里的问题。完整的功能如下:

exports.handleSubscriptionIAP_IOS_S2S = functions.https.onRequest((req, res) => {
    let data = req.body;

    console.log('received ios s2s notification with body:', data);

    let base64String = data.unified_receipt.latest_receipt;

    let boolStatus = true;
    // The user has changed the auto renewal status, store the change
    if(data.notification_type === 'DID_CHANGE_RENEWAL_STATUS') {
        boolStatus = (data.auto_renew_status === 'true');
    }

    console.log(data.notification_type);

    if(base64String) {
        var options = {
            method: 'post',
            url: 'https://buy.itunes.apple.com/verifyReceipt',
            data: ({
                "receipt-data" : base64String,
                "password" : "***",
                "exclude-old-transactions" : true
            })
        };

        var optionsSandbox = {
            method: 'post',
            url: 'https://sandbox.itunes.apple.com/verifyReceipt',
            data: ({
                "receipt-data" : base64String,
                "password" : "***",
                "exclude-old-transactions" : true
            })
        };

        return axios(options)
        .then((response) => {
            if(response.data.status === 21007) {
                return 'handle_sandbox';
            }

            // Got valid response from Apple, pass down chain
            else {
                return response;
            }
        })
        .then((response) => {
            // Send post request to sandbox endpoint
            if(response === 'handle_sandbox') {
                return axios(optionsSandbox);
            }

            // Pass response down chain
            else {
                return response;
            }
        })
        .then((response) => {
            // Handle response from Apple verifyReceipt endpoint here
            // Both production and sandbox will end up here
            // See here for values in response.data: https://developer.apple.com/documentation/appstorereceipts/responsebody/latest_receipt_info

            console.log('received ios s2s notification verify receipt response with body:', response.data);
            // Status 0 means request is valid
            if(response.data.status === 0) {
                // Get receipt info of latest receipt
                // Only one object in array is return since we exclude old transactions
                let latestReceipt = response.data.latest_receipt_info[0];

                // Save receipt into Firestore
                return db.collection('appStoreReceipts').doc(latestReceipt.original_transaction_id).set({
                    latest_receipt_info: latestReceipt,

                    expiresTimestamp: admin.firestore.Timestamp.fromMillis(parseInt(latestReceipt.expires_date_ms)),
                    originalTransactionID: latestReceipt.original_transaction_id,

                    autoRenewStatus: boolStatus,

                    base64Receipt: response.data.latest_receipt,
                }, { merge: true });
            }
            else {
                return null;
            }
        })
        .then((result) => {
            if(result) {
                return res.status(200).end();
            }
            else {
                return res.status(400).end();
            }
        })
        .catch((error) => {
            console.log('an error occured handling the subscription', error);

            return res.status(400).end();
        })
    }
    else {
        console.log('invalid receipt', data);

        return res.status(400).end();
    }
});

感谢您提供的任何帮助!

最佳答案

ECONNRESET只是意味着连接的另一端关闭了它。这可能意味着很多事情。这可能不是你的代码的错,除非你在这个请求中做了一些糟糕的事情以至于苹果决定关闭连接。如果您认为他们的做法不正确,您应该直接联系他们的支持人员。

关于node.js - 在云函数中处理 HTTP 请求时读取 ECONNRESET 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795226/

相关文章:

javascript - 多个fs.write追加到同一个文件能保证执行顺序吗?

javascript - Firestore 网络代码示例给出了无效的参数类型

ios - facebooksdk 无法快速获取 accesstoken

javascript - 如何修复 "This operation is not supported in the environment this application is running on. "location.protocol"..."错误?

android - 如何将对象从android应用程序传递到firebase云函数以完成Paypal支付功能?

node.js - 使用 Nodejs 重命名文件

angularjs - 如何使用 ng-file-upload 在express js中保存文件图像

javascript - 尝试迭代 Map 元素时出现 TypeError

node.js - 如何从nodejs中打开的包获取响应

node.js - 从云函数内的请求主体访问各个字段