Tag: exception处理

Nodejs:如何捕捉net.createServer.on(“data”,…)中的exception?

我有一个标准的套接字服务器(无HTTP)设置如下(人为): var server = net.createServer(function(c) { //'connection' listener c.on('data', function(data) { //do stuff here //some stuff can result in an exception that isn't caught anywhere downstream, //so it bubbles up. I try to catch it here. //this is the same problem as just trying to catch this: throw new Error("catch me if you can"); }); […]

在NodeJS中编写域感知函数

我试图想出一个在NodeJS中处理exception的一般(自以为是)的方式,由于性能的影响,它不使用try catch。 我也想远离像简化这样的库,试图使asynchronous代码看起来像同步代码。 似乎域名很适合这个法案,但是我想征求意见/build议,以便我build议使用它们。 这种方法是否存在重大问题? 我打算使我的大部分asynchronous函数遵循以下domainAware函数的模式: function errorinAsync(options, callback){ options = options || {}; setTimeout(function(){ return callback(new Error("This should be caught")); },1000); } function domainAware(options, callback){ if(domain.active){ d = domain.active; }else{ d = domain.create(); d.on('error', function(err){ return callback(err); }); } d.run(function(){ //Some Synchronous code that might throw an exception; var a = {b: 1, c: […]

如何捕获node.js中的zlib错误

我正在从可能被压缩的服务器上下载内容,所以我正在使用我在各种地方发现的样板文件: var file = fs.createWriteStream(filename); var request = https.get(url, function(response) { switch (response.headers['content-encoding']) { case 'gzip': response.pipe(zlib.createGunzip()).pipe(file); break; case 'deflate': response.pipe(zlib.createInflate()).pipe(file); break; default: response.pipe(file); break; } file.on('finish', function() { console.log("Done: "+url); file.close(function() {}); }); }).on('error', function(err) { // Handle errors fs.unlink(filename, function() {}); console.log(err+" "+url); }); 麻烦的是,如果HTTPS请求因networking错误而失败,我有时会得到这个exception: Error: unexpected end of file at Zlib._handle.onerror (zlib.js:363:17) […]

在node.js中重新启动

我在node.js中有一个互联网半依赖的代码 它取决于互联网更新和启动只。 与process.on('uncaughtException')我能够得到所有的互联网连接问题。 我想要的是停止脚本的方法,等待一段时间,然后从头开始重新启动应用程序。 我努力了: process.on('uncaughtException', function(err) { console.log('Caught fatal error ' + err + ', will restart in 10sec'); setTimeout(function() { var arg = ['-c', 'sh start.sh'], child = spawn('bash', arg); process.exit(); }, 10000); }); 它重新启动脚本,但在另一个实例中,这意味着当我ctrl + c杀死进程时,我的日志显示node.js的X实例在哪里运行。 (日志结束多个'用户杀死进程。杀死path/到/ app.js')我想在产卵之前杀死脚本start.sh (nb:start.sh是一个简单的bash脚本,用一些参数启动我的应用程序)。

重新启动node.js群集工作者

我有一个节点应用程序,我使用群集在多个核心上运行。 我遇到了一个问题,当我的应用程序抛出一个exception,工人死亡,不重新启动。 这是有道理的,但我想知道是否有更好的方法来处理重新启动这些工人,而不是必须监视它,或尝试/捕捉或听每一个错误。 我以前永远都用过,而且看起来像这样的东西对于集群来说是很好的。 有没有办法重新启动它们,或者在顶层添加“pokemon”exception处理,以确保我的工作人员不会在意外exception情况下死亡?

为什么使用nodejs上传exception

码 form.parse(request, function(error, fields, files) { console.log('parsing done'); fs.renameSync(files.upload.path, 'c:/tmp/test.png'); response.writeHead(200, {'Content-Type': 'text/html'}); response.write('received image:<br/>'); response.write('<img src="/show" />'); response.end(); }); exception信息 返回binding.rename(pathModule._makeLong(oldPath)), 错误:ENOENT,没有这样的文件或目录'C:\ DOCUME〜1 \ ADMINI〜1 \ LOCALS〜1 \ Temp \ esdafdace32323432a3f' 在Object.renameSync(fs.js:330:18)

如何解决在本地捕获exception的“抛出”?

在处理REST API调用的这个函数中,任何处理部分请求的被调用的函数都可能会抛出一个错误来指示错误代码应该作为响应发送。 但是,函数本身也可能发现一个错误,在这一点上它应该跳入exception处理块。 static async handleRequest(req) { try { let isAllowed = await checkIfIsAllowed(req); if (!isAllowed) { throw new ForbiddenException("You're not allowed to do that."); } let result = await doSomething(req); // can also raise exceptions sendResult(result); } catch(err) { sendErrorCode(err); } } Webstorm将突出显示以下消息: 'throw' of exception caught locally. This inspection reports any instances of […]

在量angular器转轮中捕捉错误

有没有办法来捕捉protractor的跑步者直接抛出的具体错误? 例如,一个未捕获的exception是: WARNING – pattern /var/www/smoran/repo/app/partials/app/app.controller.e2e.js did not match any files. [launcher] Process exited with error code 1 /var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:126 throw e; ^ Error: Spec patterns did not match any files. at Runner.run (/var/www/smoran/repo/node_modules/protractor/lib/runner.js:249:11) at TaskRunner.run (/var/www/smoran/repo/node_modules/protractor/lib/taskRunner.js:123:19) at createNextTaskRunner (/var/www/smoran/repo/node_modules/protractor/lib/launcher.js:220:20) at /var/www/smoran/repo/node_modules/protractor/lib/launcher.js:243:7 at _fulfilled (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:797:54) at self.promiseDispatch.done (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:826:30) at Promise.promise.promiseDispatch (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:759:13) at /var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:573:44 at flush (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:108:17) […]

Node.js:在正常的工作stream程中抛出exception是否可行?

我在我的node.js代码中使用生成器和promise,所以抛出和处理exception并不像以前那样痛苦。 所以我开始在C ++或C#中使用它们的方式开始使用exception:通常,如果我想立即退出函数,则抛出一个exception。 问题是 – 这是好的做法还是有什么缺点(performance,泄漏)?

NodeJS – 间歇性地出现“错误:此sockets已closures”

请参阅下面的错误,我正在调用一个nodeJS路由。 我在另一条路线也看到相同的exception。 两条路线都有做MongoDB操作的方法。 这是间歇性的,只发生在处理数据库中的值的两个API调用中。 { date: 'Thu Sep 15 2016 04:16:46 GMT-0500 (EST)', process: { pid: 4424, uid: 0, gid: 0, cwd: '***rootFolder***', execPath: '/usr/bin/node', version: 'v6.3.1', argv: [ '/usr/bin/node', '**rootfolder**/app.js' ], memoryUsage: { rss: 104415232, heapTotal: 77893632, heapUsed: 60997696 } }, os: { loadavg: [ 0.03564453125, 0.06103515625, 0.0625 ], uptime: 3184925 }, trace: [ […]