Skip to content

Commit

Permalink
refactor(client): nonLazyError uses console.error as the default
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Dec 11, 2020
1 parent a879d1f commit 51e5459
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
8 changes: 5 additions & 3 deletions docs/interfaces/_client_.clientoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ ___
`Optional` **onNonLazyError**: undefined \| (errorOrCloseEvent: unknown) => void

Used ONLY when the client is in non-lazy mode (`lazy = false`). When
using this mode, the errors might have no sinks to report to. To avoid
swallowing errors, or having uncaught promises; consider using `onNonLazyError`,
which will be called when either:
using this mode, the errors might have no sinks to report to; however,
to avoid swallowing errors, consider using `onNonLazyError`, which will
be called when either:
- An unrecoverable error/close event occurs
- Silent retry attempts have been exceeded

Expand All @@ -112,6 +112,8 @@ close event is labeled as fatal (read more in `retryAttempts`).
- An `Error`: some internal issue has occured, all internal errors are
fatal by nature.

**`default`** console.error

___

### retryAttempts
Expand Down
10 changes: 6 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export interface ClientOptions {
lazy?: boolean;
/**
* Used ONLY when the client is in non-lazy mode (`lazy = false`). When
* using this mode, the errors might have no sinks to report to. To avoid
* swallowing errors, or having uncaught promises; consider using `onNonLazyError`,
* which will be called when either:
* using this mode, the errors might have no sinks to report to; however,
* to avoid swallowing errors, consider using `onNonLazyError`, which will
* be called when either:
* - An unrecoverable error/close event occurs
* - Silent retry attempts have been exceeded
*
Expand All @@ -93,6 +93,8 @@ export interface ClientOptions {
* close event is labeled as fatal (read more in `retryAttempts`).
* - An `Error`: some internal issue has occured, all internal errors are
* fatal by nature.
*
* @default console.error
*/
onNonLazyError?: (errorOrCloseEvent: unknown) => void;
/**
Expand Down Expand Up @@ -173,7 +175,7 @@ export function createClient(options: ClientOptions): Client {
url,
connectionParams,
lazy = true,
onNonLazyError,
onNonLazyError = console.error,
keepAlive = 0,
retryAttempts = 5,
retryWait = async function randomisedExponentialBackoff(retries) {
Expand Down
68 changes: 58 additions & 10 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ it('should use the provided WebSocket implementation', async () => {
createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
webSocketImpl: WebSocket,
});
Expand All @@ -150,6 +151,7 @@ it('should not accept invalid WebSocket implementations', async () => {
createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
webSocketImpl: {},
}),
Expand All @@ -164,6 +166,7 @@ it('should recieve optional connection ack payload in event handler', async (don
createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
on: {
connected: (_socket, payload) => {
Expand All @@ -185,6 +188,7 @@ it('should close with error message during connecting issues', async () => {
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
on: {
connected: () => {
// the `connected` listener is called right before successful connection
Expand All @@ -211,6 +215,7 @@ it('should pass the `connectionParams` through', async () => {
let client = createClient({
url: server.url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
connectionParams: { auth: 'token' },
});
Expand All @@ -222,6 +227,7 @@ it('should pass the `connectionParams` through', async () => {
client = createClient({
url: server.url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
connectionParams: () => ({ from: 'func' }),
});
Expand All @@ -233,6 +239,7 @@ it('should pass the `connectionParams` through', async () => {
client = createClient({
url: server.url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
connectionParams: () => Promise.resolve({ from: 'promise' }),
});
Expand All @@ -247,6 +254,7 @@ it('should close the socket if the `connectionParams` rejects or throws', async
let client = createClient({
url: server.url,
retryAttempts: 0,
onNonLazyError: noop,
connectionParams: () => {
throw new Error('No auth?');
},
Expand All @@ -263,6 +271,7 @@ it('should close the socket if the `connectionParams` rejects or throws', async
client = createClient({
url: server.url,
retryAttempts: 0,
onNonLazyError: noop,
connectionParams: () => Promise.reject(new Error('No auth?')),
});

Expand All @@ -279,7 +288,11 @@ describe('query operation', () => {
it('should execute the query, "next" the result and then complete', async () => {
const { url } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub = tsubscribe(client, {
query: 'query { getValue }',
Expand All @@ -295,7 +308,11 @@ describe('query operation', () => {
it('should accept nullish value for `operationName` and `variables`', async () => {
const { url } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

// nothing
await tsubscribe(client, {
Expand All @@ -322,7 +339,11 @@ describe('subscription operation', () => {
it('should execute and "next" the emitted results until disposed', async () => {
const { url, ...server } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub = tsubscribe(client, {
query: 'subscription Ping { ping }',
Expand Down Expand Up @@ -353,7 +374,11 @@ describe('subscription operation', () => {
it('should emit results to correct distinct sinks', async () => {
const { url, ...server } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub1 = tsubscribe(client, {
query: `subscription Ping($key: String!) {
Expand Down Expand Up @@ -415,6 +440,7 @@ describe('subscription operation', () => {
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
generateID: generateIDFn,
});

Expand All @@ -429,7 +455,11 @@ describe('subscription operation', () => {
it('should dispose of the subscription on complete', async () => {
const { url, ...server } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub = tsubscribe(client, {
query: '{ getValue }',
Expand All @@ -445,7 +475,11 @@ describe('subscription operation', () => {
it('should dispose of the subscription on error', async () => {
const { url, ...server } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub = tsubscribe(client, {
query: '{ iDontExist }',
Expand All @@ -466,9 +500,12 @@ describe('subscription operation', () => {
waitForComplete,
} = await startTServer();

const sub = tsubscribe(createClient({ url, retryAttempts: 0 }), {
query: 'subscription { greetings }',
});
const sub = tsubscribe(
createClient({ url, retryAttempts: 0, onNonLazyError: noop }),
{
query: 'subscription { greetings }',
},
);
await waitForOperation();

for (const client of clients) {
Expand All @@ -487,7 +524,11 @@ describe('"concurrency"', () => {
it('should dispatch and receive messages even if one subscriber disposes while another one subscribes', async () => {
const { url, ...server } = await startTServer();

const client = createClient({ url, retryAttempts: 0 });
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub1 = tsubscribe(client, {
query: 'subscription { ping }',
Expand Down Expand Up @@ -522,6 +563,7 @@ describe('lazy', () => {
createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
});

Expand All @@ -537,6 +579,7 @@ describe('lazy', () => {
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
on: {
connected: () => resolve(client),
},
Expand All @@ -555,6 +598,7 @@ describe('lazy', () => {
url,
lazy: true, // default
retryAttempts: 0,
onNonLazyError: noop,
});

await server.waitForClient(() => {
Expand Down Expand Up @@ -582,6 +626,7 @@ describe('lazy', () => {
url,
lazy: true, // default
retryAttempts: 0,
onNonLazyError: noop,
});

await server.waitForClient(() => {
Expand Down Expand Up @@ -622,6 +667,7 @@ describe('lazy', () => {
lazy: true, // default
keepAlive: 20,
retryAttempts: 0,
onNonLazyError: noop,
});

const sub = tsubscribe(client, {
Expand Down Expand Up @@ -673,6 +719,7 @@ describe('reconnecting', () => {
createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
}),
{
query: 'subscription { ping }',
Expand Down Expand Up @@ -883,6 +930,7 @@ describe('events', () => {
const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
on: {
connecting: connectingFn,
connected: connectedFn,
Expand Down

0 comments on commit 51e5459

Please sign in to comment.