Skip to content

Commit

Permalink
fix(client): Close with error message during connecting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Nov 7, 2020
1 parent f74496d commit f8ecdd7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ export function createClient(options: ClientOptions): Client {
emitter.emit('connected', socket, message.payload); // connected = socket opened + acknowledged
return resolve();
} catch (err) {
socket.close(4400, err);
socket.close(
4400,
err instanceof Error ? err.message : new Error(err).message,
);
}
};

Expand Down
27 changes: 27 additions & 0 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,33 @@ it('should recieve optional connection ack payload in event handler', async (don
});
});

it('should close with error message during connecting issues', async () => {
const { url } = await startTServer();

const someerr = new Error('Welcome');
const client = createClient({
url,
retryAttempts: 0,
on: {
connected: () => {
// the `connected` listener is called right before successful connection
throw someerr;
},
},
});

const sub = tsubscribe(client, {
query: 'query { getValue }',
});

await sub.waitForError((err) => {
const event = err as CloseEvent;
expect(event.code).toBe(4400);
expect(event.reason).toBe('Welcome');
expect(event.wasClean).toBeTruthy();
});
});

describe('query operation', () => {
it('should execute the query, "next" the result and then complete', async () => {
const { url } = await startTServer();
Expand Down

0 comments on commit f8ecdd7

Please sign in to comment.