Skip to content

Commit

Permalink
fix(core): Handle websocket connection error more gracefully in task …
Browse files Browse the repository at this point in the history
…runners (#11635)
  • Loading branch information
ivov authored Nov 11, 2024
1 parent 835fbfe commit af7d6e6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/@n8n/task-runner/src/task-runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationError } from 'n8n-workflow';
import { ApplicationError, ensureError } from 'n8n-workflow';
import { nanoid } from 'nanoid';
import { type MessageEvent, WebSocket } from 'ws';

Expand Down Expand Up @@ -88,6 +88,24 @@ export abstract class TaskRunner {
},
maxPayload: opts.maxPayloadSize,
});

this.ws.addEventListener('error', (event) => {
const error = ensureError(event.error);

if (
'code' in error &&
typeof error.code === 'string' &&
['ECONNREFUSED', 'ENOTFOUND'].some((code) => code === error.code)
) {
console.error(
`Error: Failed to connect to n8n. Please ensure n8n is reachable at: ${opts.n8nUri}`,
);
process.exit(1);
} else {
console.error(`Error: Failed to connect to n8n at ${opts.n8nUri}`);
console.error('Details:', event.message || 'Unknown error');
}
});
this.ws.addEventListener('message', this.receiveMessage);
this.ws.addEventListener('close', this.stopTaskOffers);
}
Expand Down

0 comments on commit af7d6e6

Please sign in to comment.