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

feat(error-handling): add canister error tracking for query and update calls #6443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
67 changes: 59 additions & 8 deletions frontend/src/lib/services/utils.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getAuthenticatedIdentity,
getCurrentIdentity,
} from "$lib/services/auth.services";
import { canistersErrorsStore } from "$lib/stores/canisters-errors.store";
import { logWithTimestamp } from "$lib/utils/dev.utils";
import type { Identity } from "@dfinity/agent";

Expand All @@ -26,6 +27,63 @@ export type QueryAndUpdateIdentity = "authorized" | "anonymous" | "current";

let lastIndex = 0;

type QueryAndUpdateParams<R, E> = {
request: (options: { certified: boolean; identity: Identity }) => Promise<R>;
onLoad: QueryAndUpdateOnResponse<R>;
logMessage: string;
onError?: QueryAndUpdateOnError<E>;
strategy?: QueryAndUpdateStrategy;
identityType?: QueryAndUpdateIdentity;
};

const isQueryCallOfAQueryAndUpdateCall = (
strategy: QueryAndUpdateStrategy,
certified: boolean
) => strategy === "query_and_update" && certified === false;

export const queryAndUpdateWithCanisterErrorTrack = async <R, E>({
request,
onLoad,
onError,
strategy,
logMessage,
identityType = "authorized",
canisterId,
// TOOD(yhabib): Change type of canisterId from string to CanisterId
}: QueryAndUpdateParams<R, E> & { canisterId: string }): Promise<void> => {
const customOnLoad: QueryAndUpdateOnResponse<R> = ({
certified,
strategy,
response,
}) => {
if (!isQueryCallOfAQueryAndUpdateCall(strategy, certified))
canistersErrorsStore.delete(canisterId);

onLoad({ certified, strategy, response });
};

const customOnError: QueryAndUpdateOnError<E> = ({
certified,
strategy,
error,
identity,
}) => {
if (!isQueryCallOfAQueryAndUpdateCall(strategy, certified))
canistersErrorsStore.set({ canisterId, rawError: error });
Copy link
Contributor

Choose a reason for hiding this comment

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

Not every error is an indication that there's something wrong with the canister.

The best example I can think of is that the ckBTC minter, when you request completed transactions, if there are no completed transactions, it will throw an error which includes pending transactions:

if (!(error instanceof MinterNoNewUtxosError)) {

This is how we fetch completed and pending transactions. This is normal operations and doesn't indicate that there's anything wrong with the canister or the request.

Probably there are other (perhaps less clear cut) cases where we might get an error from a canister but not want to put it in this store.


onError?.({ certified, strategy, error, identity });
};

return queryAndUpdate({
request,
onLoad: customOnLoad,
onError: customOnError,
strategy,
logMessage,
identityType,
});
};

/**
* Depending on the strategy makes one or two requests (QUERY and UPDATE in parallel).
* The returned promise notify when first fetched data are available.
Expand All @@ -40,14 +98,7 @@ export const queryAndUpdate = async <R, E>({
strategy,
logMessage,
identityType = "authorized",
}: {
request: (options: { certified: boolean; identity: Identity }) => Promise<R>;
onLoad: QueryAndUpdateOnResponse<R>;
logMessage: string;
onError?: QueryAndUpdateOnError<E>;
strategy?: QueryAndUpdateStrategy;
identityType?: QueryAndUpdateIdentity;
}): Promise<void> => {
}: QueryAndUpdateParams<R, E>): Promise<void> => {
let certifiedDone = false;
let requests: Array<Promise<void>>;
let logPrefix: string;
Expand Down
91 changes: 90 additions & 1 deletion frontend/src/tests/lib/services/utils.services.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { queryAndUpdate } from "$lib/services/utils.services";
import {
queryAndUpdate,
queryAndUpdateWithCanisterErrorTrack,
} from "$lib/services/utils.services";
import { canistersErrorsStore } from "$lib/stores/canisters-errors.store";
import * as devUtils from "$lib/utils/dev.utils";
import {
mockIdentity,
Expand All @@ -7,6 +11,7 @@ import {
} from "$tests/mocks/auth.store.mock";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import { tick } from "svelte";
import { get } from "svelte/store";

describe("api-utils", () => {
describe("queryAndUpdate", () => {
Expand Down Expand Up @@ -336,4 +341,88 @@ describe("api-utils", () => {
});
});
});

describe("queryAndUpdateWithCanisterErrorTrack", () => {
const TEST_CANISTER_ID = "ryjl3-tyaaa-aaaaa-aaaba-cai";

beforeEach(() => {
resetIdentity();
});

it("should delete canister error on successful response", async () => {
const response = { data: "test" };
const request = vi.fn().mockResolvedValue(response);
const onLoad = vi.fn();

canistersErrorsStore.set({
canisterId: TEST_CANISTER_ID,
rawError: "initial error",
});

await queryAndUpdateWithCanisterErrorTrack({
request,
onLoad,
logMessage: "test-log",
canisterId: TEST_CANISTER_ID,
});

expect(get(canistersErrorsStore)).toEqual({});
});

it("should set canister error on error response", async () => {
const error = new Error("test error");
const request = vi.fn().mockRejectedValue(error);
const onLoad = vi.fn();
const onError = vi.fn();

await queryAndUpdateWithCanisterErrorTrack({
request,
onLoad,
onError,
logMessage: "test-log",
canisterId: TEST_CANISTER_ID,
});

expect(get(canistersErrorsStore)).toEqual({
[TEST_CANISTER_ID]: { raw: error },
});
});

it("should not update error store for query call in query_and_update strategy", async () => {
let resolveUpdate: (value: unknown) => void;
let rejectQuery: (value: unknown) => void;
const queryError = new Error("query error");

const request = vi
.fn()
.mockImplementation(({ certified }: { certified: boolean }) =>
certified
? new Promise((resolve) => (resolveUpdate = resolve))
: new Promise((_, reject) => (rejectQuery = reject))
);
const onLoad = vi.fn();
const onError = vi.fn();

queryAndUpdateWithCanisterErrorTrack({
request,
onLoad,
onError,
logMessage: "test-log",
canisterId: TEST_CANISTER_ID,
strategy: "query_and_update",
});

await runResolvedPromises();

rejectQuery(queryError);
await runResolvedPromises();

expect(get(canistersErrorsStore)).toEqual({});

resolveUpdate({});
await runResolvedPromises();

expect(get(canistersErrorsStore)).toEqual({});
});
});
});
Loading