Skip to content

Commit

Permalink
fix(server): Log internal errors to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 19, 2021
1 parent 8d13c9e commit 6ddf0d1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import {
import { startWSTServer as startTServer } from './utils';
import { ExecutionResult } from 'graphql';

// silence console.error calls for nicer tests overview
const consoleError = console.error;
beforeAll(() => {
console.error = () => {
// silence
};
});
afterAll(() => {
console.error = consoleError;
});

// simulate browser environment for easier client testing
beforeEach(() => {
Object.assign(global, {
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ import {
import { schema, schemaConfig } from './fixtures/simple';
import { createTClient, startWSTServer as startTServer } from './utils';

// silence console.error calls for nicer tests overview
const consoleError = console.error;
beforeAll(() => {
console.error = () => {
// silence
};
});
afterAll(() => {
console.error = consoleError;
});

/**
* Tests
*/
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import {
FastifyExtra,
} from './utils';

// silence console.error calls for nicer tests overview
const consoleError = console.error;
beforeAll(() => {
console.error = () => {
// silence
};
});
afterAll(() => {
console.error = consoleError;
});

for (const { tServer, startTServer } of tServers) {
describe(tServer, () => {
it('should allow connections with valid protocols only', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/use/fastify-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export function makeHandler<
try {
await cb(String(event));
} catch (err) {
console.error(
'Internal error occurred during message handling. ' +
'Please check your implementation.',
err,
);
socket.close(
CloseCode.InternalServerError,
// close reason should fit in one frame https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
Expand Down
5 changes: 5 additions & 0 deletions src/use/uWebSockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ export function makeBehavior<
try {
await client.handleMessage(Buffer.from(message).toString());
} catch (err) {
console.error(
'Internal error occurred during message handling. ' +
'Please check your implementation.',
err,
);
socket.end(
CloseCode.InternalServerError,
// close reason should fit in one frame https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
Expand Down
5 changes: 5 additions & 0 deletions src/use/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export function useServer<
try {
await cb(String(event));
} catch (err) {
console.error(
'Internal error occurred during message handling. ' +
'Please check your implementation.',
err,
);
socket.close(
CloseCode.InternalServerError,
// close reason should fit in one frame https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
Expand Down

0 comments on commit 6ddf0d1

Please sign in to comment.