我正在创建一个应用程序,它(现在)将文件作为输入并返回其哈希值(sha256)。
只要用户使用文件作为输入,它就可以正常工作,但是如果他放入其他内容(例如字符串),应用程序会静默失败(应用程序日志中有堆栈跟踪,但 Zapier 没有显示任何特别内容)并返回错误的哈希。
我不认为我的代码可以处理错误并且堆栈非常模糊:
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Log │ Unhandled error: Error: Error: Could not find the method to call: authentication.test │
│ │ What happened: │
│ │ Executing authentication.test with bundle │
│ │ Error: Could not find the method to call: authentication.test │
│ │ Error: Error: Could not find the method to call: authentication.test │
│ │ at execute (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/execute.js:83:11) │
│ │ at input (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/create-command-handler.js:26:12) │
│ │ at Object.beforeMiddleware.then.newInput (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/middleware.js:90:22) │
│ │ at bound (domain.js:280:14) │
│ │ at Object.runBound (domain.js:293:12) │
│ │ at Object.tryCatcher (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/util.js:16:23) │
│ │ at Promise._settlePromiseFromHandler (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/promise.js:512:31) │
│ │ at Promise._settlePromise (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/promise.js:569:18) │
│ │ at Promise._settlePromise0 (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/promise.js:614:10) │
│ │ at Promise._settlePromises (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/promise.js:693:18) │
│ │ at Async._drainQueue (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/async.js:133:16) │
│ │ at Async._drainQueues (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/async.js:143:10) │
│ │ at Immediate.Async.drainQueues (:censored:9:d1ba0cf2aa:/node_modules/bluebird/js/release/async.js:17:14) │
│ │ at runCallback (timers.js:672:20) │
│ │ at tryOnImmediate (timers.js:645:5) │
│ │ at processImmediate [as _immediateCallback] (timers.js:617:5) │
│ Version │ 1.0.7 │
│ Step │ │
│ Timestamp │ 2018-05-24T03:57:57-05:00 │
└───────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
我的代码本质上是“文件”示例应用程序的副本,creates/uploadFile.js
文件替换为:
const request = require('request');
const crypto = require('crypto');
function hashStream(stream) {
const hash = crypto.createHash('sha256').setEncoding('hex');
return new Promise((resolve, reject) => {
stream.pipe(hash)
.on('finish', () => resolve(hash.read()))
.on('error', reject)
})
}
function hashFile(z, bundle) {
const fileStream = request(bundle.inputData.file);
return hashStream(fileStream).then((hash) => ({hash}));
};
module.exports = {
key: 'hashFile',
noun: 'File',
display: {
label: 'Hash File',
description: 'Performs a sha256 on file.'
},
operation: {
inputFields: [
{key: 'file', required: true, type: 'file', label: 'File'},
],
perform: hashFile,
sample: { filename: 'example.pdf' },
outputFields: [
{key: 'hash', type: 'string', label: 'Hash'}
],
}
};
更新:我最终发现了我的错误:我假设该request
函数会在失败时抛出错误。
所以这个hashFile
函数肯定是在散列错误页面。
更改hashFile
功能解决了问题:
function hashFile(z, bundle) {
return new Promise((resolve, reject) => {
request(bundle.inputData.file)
.on('response', function ({statusCode}) {
if (200 === statusCode) {
hashStream(this).then((hash) => resolve({hash}));
} else {
reject(new Error(`Invalid status code ${statusCode}`));
}
})
.on('error', (err) => reject(err))
})
}
但是:我无法捕捉到“未处理的错误”;我试过了
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at:', p, 'reason:', reason);
process.exit(1);
});
但我认为 Zapier 引擎阻止了这种技巧,因为它没有效果......