gabrielcsapo / gabrielcsapo/node-git-server

Can not catch error when start listen

未关闭
#69 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
TypeScript
星标
262
派生
81
PR 合并指标
30 天内没有已合并 PR

描述

The JSDoc of the `Git.listen()` method states that "the function to call when server is started or error has occured" in the description of the `callback` argument.

https://github.com/gabrielcsapo/node-git-server/blob/5279f64b200b3d8e8f3d537c28c86793033e6285/lib/git.js#L429-L441

However, in reality, if the server fails to start with an error, the callback is not called.
In addition, **this error cannot be catched even with [try...catch statement](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/try...catch)**.

> The Server object is a Node.js EventEmitter. As with all EventEmitter's, _most_ errors are passed to the `'error'` event. If no handler is registered for the `'error'` event, those bubble up to be thrown. `try/catch` does not work because when listen is attempted, any errors that occur are caught and emitted on the `'error'` event using `process.nextTick()` -- that is, by the time the error is actually reported, the `try/catch` block has already exited. As a fallback, you can register an `'uncaughtException'` handler on `process` as a catch all for any unhandled errors that occur on any EventEmitter object, but it's best to simply set the `'error'` callback on the server object.
>
> _Originally posted by @jasnell in https://github.com/expressjs/express/issues/2856#issuecomment-172566787_

The best way to resolve this problem is to register the `error` event on [the `http.Server` object](https://nodejs.org/docs/latest-v6.x/api/http.html#http_class_http_server) to get the error.
Currently (version 0.6.1) we can get the error in the following ways:

```js
const Server = require('node-git-server');

const repos = new Server('path/to/tmp', {
autoCreate: true
});
const port = process.env.PORT || 7005;

// ...

repos.listen(port, () => {
console.log(`Success!`)
});
repos.server.on('error', error => {
console.error(`Error: ${error}`);
});
```

However, this solution requires the use of [the `server` property](https://github.com/gabrielcsapo/node-git-server/blob/5279f64b200b3d8e8f3d537c28c86793033e6285/lib/git.js#L449-L451) that are not in the documentation.
In addition, this method is not intuitive. The user cannot catch the error in the callback, nor in the try...catch statement. This solution requires knowledge of Node.js built-in http/https modules.

The ideal solution is a change that allows the function in the `callback` argument to catch the error.
To do this, this project needs to make the following changes to the `Git.listen()` method:

```diff
listen(port, options, callback) {
const self = this;
if(typeof options == 'function' || !options) {
callback = options;
options = { type: 'http' };
}
const createServer = options.type == 'http' ? http.createServer : https.createServer.bind(this, options);

this.server = createServer(function(req, res) {
self.handle(req, res);
});

+ this.server.on('error', callback);
+
this.server.listen(port, callback);
return this;
}
```

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 lib/git.js 中 Git.listen() 附近开始,重点查看第 429-451 行的服务器创建和 listen 调用。检查当前如何传递启动错误,并将其与文档中说明的回调行为进行比较。当 listen 回调能够一致地接收启动错误,并且对外暴露的服务器错误行为与文档保持一致时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。