node异常处理

参考:
node API
【译】NodeJS错误处理最佳实践

具体操作

1.最基本同步代码处理
// Throws with a ReferenceError because z is undefined
try {
  const m = 1;
  const n = m + z;
} catch (err) {
  // Handle the error here.
}
2.简单异步处理
//方案1
const fs = require('fs');
fs.readFile('a file that does not exist', (err, data) => {
  if (err) {
    console.error('There was an error reading the file!', err);
    return;
  }
  // Otherwise handle the data
});
//方案2
const fs = require('fs');

function nodeStyleCallback(err, data) {
 if (err) {
   console.error('There was an error', err);
   return;
 }
 console.log(data);
}

fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);
fs.readFile('/some/file/that/does-exist', nodeStyleCallback)
//方案3
// THIS WILL NOT WORK:
const fs = require('fs');

try {
  fs.readFile('/some/file/that/does-not-exist', (err, data) => {
    // mistaken assumption: throwing here...
    if (err) {
      throw err;
    }
  });
} catch(err) {
  // This will not catch the throw!
  console.log(err);
}
3.稍微复杂一点异步处理
const net = require('net');
const connection = net.connect('localhost');

// Adding an 'error' event handler to a stream:
connection.on('error', (err) => {
  // If the connection is reset by the server, or if it can't
  // connect at all, or on any sort of error encountered by
  // the connection, the error will be sent here.
  console.error(err);
});

connection.pipe(process.stdout);
4.setImmediate等处理
const EventEmitter = require('events');
const ee = new EventEmitter();

setImmediate(() => {
  // This will crash the process because no 'error' event
  // handler has been added.
  ee.emit('error', new Error('This will crash'));
});

自定义异常

至少需要这些属性:

name:用于在程序里区分众多的错误类型(例如参数非法和连接失败) MDN e.name默认是”Error”

message:一个供人类阅读的错误消息。对可能读到这条消息的人来说这应该已经足够完整。如果你从更底层的地方传递了一个错误,你应该加上一些信息来说明你在做什么。怎么包装异常请往下看。()

stack:一般来讲不要随意扰乱堆栈信息。甚至不要增强它。V8引擎只有在这个属性被读取的时候才会真的去运算,以此大幅提高处理异常时候的性能。如果你读完再去增强它,结果就会多付出代价,哪怕调用者并不需要堆栈信息。

Error name命名约定

强烈建议你在发生错误的时候用这些名字来保持和Node核心以及Node插件的一致。这些大部分不会和某个给定的异常对应,但是出现疑问的时候,你应该包含任何看起来有用的信息,即从编程上也从自定义的错误消息上。【表】。
参见该文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node.js 中使用 Thrift 进行开发时,异常处理是非常重要的一部分。下面是一些基于 Thrift 的 Node.js 异常处理的技巧: 1. 在客户端和服务器端都需要处理异常。在客户端,您可以使用 try-catch 块来捕获 Thrift 调用中的异常。在服务器端,您可以为每个服务实现设置错误处理程序来捕获异常。您可以使用以下代码在服务器端设置异常处理程序: ``` var myServiceHandler = { myMethod: function (params, result) { try { // Service implementation } catch (e) { // Handle errors result(new thrift.TApplicationException(thrift.TApplicationException.INTERNAL_ERROR, "Internal error")); } } }; var processor = new thrift.Processor(myServiceHandler); ``` 2. 在客户端和服务器端都应该使用 TApplicationException 类处理异常。TApplicationException 类可用于传递错误消息和错误代码。在客户端,您可以使用此类来获取有关远程服务调用的错误信息。在服务器端,您可以使用此类来向客户端传递错误信息。 3. 在 Thrift 调用中,您可以使用 Promise 或回调函数来处理异步调用。在使用 Promise 时,您可以使用 then() 和 catch() 方法来处理成功和失败的情况。在使用回调函数时,您可以在回调函数中处理异常。以下是使用 Promise 处理异常的示例代码: ``` var client = new myServiceClient(connection); client.myMethod(params) .then(function (result) { // Handle success }) .catch(function (error) { // Handle error }); ``` 4. 最后,建议使用日志记录库来记录异常信息。这将有助于您诊断问题并了解哪些异常最频繁发生。 希望以上技巧能够帮助您在 Node.js 中正确处理 Thrift 异常

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值