我有一个nodejs C ++模块,我想了解如何报告初始化错误。所以这个样板是:
vagrant@precise64:~/elasticsearch/bin$ ./elasticsearch
[2016-04-03 10:55:01,423][INFO ][node ] [Araña] version[2.3.0], pid[6748], build[8371be8/2016-03-29T07:54:48Z]
[2016-04-03 10:55:01,424][INFO ][node ] [Araña] initializing ...
[2016-04-03 10:55:02,296][INFO ][plugins ] [Araña] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2016-04-03 10:55:02,328][INFO ][env ] [Araña] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [35.1gb], net total_space [39.4gb], spins? [possibly], types [ext4]
[2016-04-03 10:55:02,328][INFO ][env ] [Araña] heap size [1015.6mb], compressed ordinary object pointers [true]
[2016-04-03 10:55:02,329][WARN ][env ] [Araña] max file descriptors [4096] for elasticsearch process likely too low, consider increasing to at least [65536]
[2016-04-03 10:55:05,107][INFO ][node ] [Araña] initialized
[2016-04-03 10:55:05,108][INFO ][node ] [Araña] starting ...
[2016-04-03 10:55:05,228][INFO ][transport ] [Araña] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2016-04-03 10:55:05,238][INFO ][discovery ] [Araña] elasticsearch/gf23pBqpSs2wDrLswVeSyQ
[2016-04-03 10:55:08,313][INFO ][cluster.service ] [Araña] new_master {Araña}{gf23pBqpSs2wDrLswVeSyQ}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2016-04-03 10:55:08,341][INFO ][http ] [Araña] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2016-04-03 10:55:08,342][INFO ][node ] [Araña] started
[2016-04-03 10:55:08,366][INFO ][gateway ] [Araña] recovered [0] indices into cluster_state
^C[2016-04-03 10:55:22,870][INFO ][node ] [Araña] stopping ...
[2016-04-03 10:55:22,884][INFO ][node ] [Araña] stopped
[2016-04-03 10:55:22,885][INFO ][node ] [Araña] closing ...
[2016-04-03 10:55:22,899][INFO ][node ] [Araña] closed
如果extern "C" void Initialize(v8::Handle<v8::Object> instance)
{
// Do initialization stuff
}
NODE_MODULE(SomeModuleType, Initialize)
没有返回类型,我怎么能在Initialize
内报告错误,因为它是extern "C"
而不允许我抛出?我应该调用一些具体的回调吗?
此外,从暴露给nodejs的回调方法报告错误的最佳做法是什么?返回null对象是常见的做法,还是可以扔到那里,这会在javascript端显示为异常?
答案 0 :(得分:1)
在潜伏在nodejs源码之后,我发现了这一点,但是仍然欢迎评论,关于这是否是一个好方法:
extern "C" void Initialize(v8::Handle<v8::Object> instance)
{
try
{
// Do something unsafe
}
catch (std::exception& Exception)
{
node::Environment::GetCurrent(v8::Isolate::GetCurrent())->ThrowError("module initialization failed");
}
}
如果我只是重新抛出异常,那么在调试器下执行就不是我想要的了。