Skip to content

Commit

Permalink
Only log server errors at the error level (#677)
Browse files Browse the repository at this point in the history
* Do not log 4xx as server errors

* Connection ordering and teardown adjustments
  • Loading branch information
goto-bus-stop authored Nov 29, 2024
1 parent 07405b7 commit ff66f07
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/HttpApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ async function errorHandling(uw) {
uw.logger.debug({ ns: 'uwave:http-api' }, 'setup HTTP error handling');
uw.httpApi.use(errorHandler({
onError(_req, error) {
if ('status' in error && typeof error.status === 'number' && error.status >= 400 && error.status < 500) {
return;
}

uw.logger.error({ err: error, ns: 'uwave:http-api' });
},
}));
Expand Down
14 changes: 8 additions & 6 deletions src/SocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ class SocketServer {
/**
* Create a socket server.
*
* @param {import('./Uwave.js').default} uw üWave Core instance.
* @param {import('./Uwave.js').Boot} uw üWave Core instance.
* @param {object} options Socket server options.
* @param {number} [options.timeout] Time in seconds to wait for disconnected
* users to reconnect before removing them.
* @param {Buffer|string} options.secret
* @param {import('http').Server | import('https').Server} [options.server]
* @param {number} [options.port]
* @private
*/
constructor(uw, options) {
if (!uw) {
Expand Down Expand Up @@ -168,9 +169,7 @@ class SocketServer {
port: options.server ? undefined : options.port,
});

this.#redisSubscription.subscribe('uwave').catch((error) => {
this.#logger.error(error);
});
uw.use(() => this.#redisSubscription.subscribe('uwave'));
this.#redisSubscription.on('message', (channel, command) => {
// this returns a promise, but we don't handle the error case:
// there is not much we can do, so just let node.js crash w/ an unhandled rejection
Expand All @@ -189,6 +188,9 @@ class SocketServer {
}, ms('10 seconds'));

this.#recountGuests = debounce(() => {
if (this.#closing) {
return;
}
this.#recountGuestsInternal().catch((error) => {
this.#logger.error({ err: error }, 'counting guests failed');
});
Expand Down Expand Up @@ -697,11 +699,11 @@ class SocketServer {
connection.close();
}

this.#recountGuests.cancel();

const closeWsServer = promisify(this.#wss.close.bind(this.#wss));
await closeWsServer();
await this.#redisSubscription.quit();

this.#recountGuests.cancel();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/errors/ValidationError.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ValidationError extends UwaveError {
this.expose = true;
this.name = 'ValidationError';
this.code = 'SCHEMA_VALIDATION_FAILED';
this.status = 400;

this.errors = errors;
}
Expand Down
2 changes: 1 addition & 1 deletion test/utils/createUwave.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function createUwave(name, options) {
sqlite: ':memory:',
secret: Buffer.from(`secret_${name}`),
logger: {
level: 'silent',
level: 'error',
},
});

Expand Down

0 comments on commit ff66f07

Please sign in to comment.