Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try catch subscription handlers #1019

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/bright-squids-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@osdk/client": patch
"@osdk/api": patch
---

Try-catches handlers called during subscription
5 changes: 4 additions & 1 deletion etc/api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ export interface ObjectSet<Q extends ObjectOrInterfaceDefinition = any, _UNUSED
export interface ObjectSetListener<O extends ObjectOrInterfaceDefinition, P extends PropertyKeys<O> = PropertyKeys<O>> {
// Warning: (ae-forgotten-export) The symbol "ObjectUpdate" needs to be exported by the entry point index.d.ts
onChange?: (objectUpdate: ObjectUpdate<O, P>) => void;
onError?: (errors: Array<any>) => void;
onError?: (errors: {
subscriptionClosed: boolean;
error: any;
}) => void;
onOutOfDate?: () => void;
onSuccessfulSubscription?: () => void;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/objectSet/ObjectSetListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface ObjectSetListener<
/**
* There was a fatal error with the subscription process. The subscription will close or will not be established.
*/
onError?: (errors: Array<any>) => void;
onError?: (errors: { subscriptionClosed: boolean; error: any }) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Settled on a boolean here, the alternative was an enum to also be able to display the "I already tried to reconnect for you case" but I think that adds unnecessary complexity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I need to make it isSubscriptionClosed?

}

type ObjectUpdate<
Expand Down
19 changes: 19 additions & 0 deletions packages/client/src/objectSet/ObjectSetListenerWebsocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ describe("ObjectSetListenerWebsocket", async () => {
});
});

describe("correctly try catches errors in handlers", () => {
beforeEach(() => {
listener.onSuccessfulSubscription.mockImplementationOnce(() => {
throw new Error("I am an error");
});
respondSuccessToSubscribe(ws, subReq1);
});
afterEach(() => {
listener.onSuccessfulSubscription.mockReset();
});

it("should call onError", async () => {
expect(listener.onError).toHaveBeenCalled();
expect(listener.onError.mock.calls[0][0].subscriptionClosed).toBe(
false,
);
});
});

describe("successfully subscribed", () => {
beforeEach(() => {
respondSuccessToSubscribe(ws, subReq1);
Expand Down
44 changes: 35 additions & 9 deletions packages/client/src/objectSet/ObjectSetListenerWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class ObjectSetListenerWebsocket {
}
} catch (error) {
this.#logger?.error(error, "Error in #initiateSubscribe");
sub.listener.onError([error]);
sub.listener.onError({ subscriptionClosed: true, error });
}
}

Expand Down Expand Up @@ -454,7 +454,12 @@ export class ObjectSetListenerWebsocket {

for (const osdkObject of osdkObjectsWithReferenceUpdates) {
if (osdkObject != null) {
sub.listener.onChange?.(osdkObject);
try {
sub.listener.onChange?.(osdkObject);
} catch (error) {
this.#logger?.error(error, "Error in onChange callback");
sub.listener.onError({ subscriptionClosed: false, error });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if an error happens while in onError the whole thing still breaks, no? We need a fallback in that case.

}
}
}

Expand Down Expand Up @@ -482,14 +487,25 @@ export class ObjectSetListenerWebsocket {

for (const osdkObject of osdkObjects) {
if (osdkObject != null) {
sub.listener.onChange?.(osdkObject);
try {
sub.listener.onChange?.(osdkObject);
} catch (error) {
this.#logger?.error(error, "Error in onChange callback");
sub.listener.onError({ subscriptionClosed: false, error });
}
}
}
};

#handleMessage_refreshObjectSet = (payload: RefreshObjectSet) => {
const sub = this.#subscriptions.get(payload.id);
invariant(sub, `Expected subscription id ${payload.id}`);
try {
sub.listener.onOutOfDate();
} catch (error) {
this.#logger?.error(error, "Error in onOutOfDate callback");
sub.listener.onError({ subscriptionClosed: false, error });
}
sub.listener.onOutOfDate();
};

Expand All @@ -508,7 +524,10 @@ export class ObjectSetListenerWebsocket {

switch (response.type) {
case "error":
sub.listener.onError(response.errors);
sub.listener.onError({
subscriptionClosed: true,
error: response.errors,
});
this.#unsubscribe(sub, "error");
break;

Expand All @@ -532,20 +551,27 @@ export class ObjectSetListenerWebsocket {
sub.subscriptionId = response.id;
this.#subscriptions.set(sub.subscriptionId, sub); // future messages come by this subId
}
if (shouldFireOutOfDate) sub.listener.onOutOfDate();
else sub.listener.onSuccessfulSubscription();
try {
if (shouldFireOutOfDate) sub.listener.onOutOfDate();
else sub.listener.onSuccessfulSubscription();
} catch (error) {
this.#logger?.error(
error,
"Error in onOutOfDate or onSuccessfulSubscription callback",
);
sub.listener.onError({ subscriptionClosed: false, error });
}
break;
default:
const _: never = response;
sub.listener.onError(response);
sub.listener.onError({ error: response, subscriptionClosed: true });
}
}
};

#handleMessage_subscriptionClosed(payload: SubscriptionClosed) {
const sub = this.#subscriptions.get(payload.id);
invariant(sub, `Expected subscription id ${payload.id}`);
sub.listener.onError([payload.cause]);
sub.listener.onError({ subscriptionClosed: true, error: payload.cause });
this.#unsubscribe(sub, "error");
}

Expand Down