Skip to content

Commit

Permalink
improved error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jun 14, 2022
1 parent cfc92e4 commit 0230ff2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.14.3

- Fixed error logging in WebSocket proxies
- Added forwarding of headers to WebSocket proxies
- Updated dependencies

Expand Down
17 changes: 10 additions & 7 deletions src/server/injectors/proxy-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class ProxyInjector implements KrasInjector {
rejectUnauthorized: false,
headers,
});
ws.on('error', (err) => core.emit('error', err));
ws.on('error', (err) => this.logError(err));
ws.on('open', () => {
open = true;

Expand All @@ -124,13 +124,13 @@ export default class ProxyInjector implements KrasInjector {
});
ws.on('message', (data) => {
core.emit('message', { content: data, from: url, to: e.id, remote: true });
e.ws.send(data, (err: Error) => core.emit('error', err));
e.ws.send(data, (err: Error) => this.logError(err));
});
e.ws.on('message', (data: WebSocket.Data) => {
core.emit('message', { content: data, to: url, from: e.id, remote: false });

if (open) {
ws.send(data, (err) => core.emit('error', err));
ws.send(data, (err) => this.logError(err));
} else {
buffer.push({
time: Date.now(),
Expand Down Expand Up @@ -214,6 +214,12 @@ export default class ProxyInjector implements KrasInjector {
return headers;
}

logError(err: any) {
if (err) {
this.core.emit('error', err);
}
}

handle(req: KrasRequest): Promise<KrasAnswer> | KrasAnswer {
const [target] = this.connectors.filter((m) => m.target === req.target);

Expand All @@ -236,10 +242,7 @@ export default class ProxyInjector implements KrasInjector {
redirect: this.config.followRedirect,
},
(err, ans) => {
if (err) {
this.core.emit('error', err);
}

this.logError(err);
resolve(ans);
},
),
Expand Down

0 comments on commit 0230ff2

Please sign in to comment.