Skip to content

Commit

Permalink
Add TooManyImportedTokensError
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrasinskis committed Jul 25, 2024
1 parent 3fe68df commit e5d7a96
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions frontend/src/lib/canisters/nns-dapp/nns-dapp.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ProposalPayloadNotFoundError,
ProposalPayloadTooLargeError,
SubAccountLimitExceededError,
TooManyImportedTokensError,
UnknownProposalPayloadError,
} from "./nns-dapp.errors";
import type { NNSDappService } from "./nns-dapp.idl";
Expand Down Expand Up @@ -359,6 +360,11 @@ export class NNSDappCanister {
if ("AccountNotFound" in response) {
throw new AccountNotFoundError("error__account.not_found");
}
if ("TooManyImportedTokens" in response) {
throw new TooManyImportedTokensError("error__imported_tokens.too_many", {
$limit: response.TooManyImportedTokens?.limit.toString(),
});
}
// Edge case
throw new Error(
`Error setting imported tokens ${JSON.stringify(response)}`
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/lib/canisters/nns-dapp/nns-dapp.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export class AccountNotFoundError extends AccountTranslateError {
}
}

export class TooManyImportedTokensError extends AccountTranslateError {
constructor(message: string, substitutions?: I18nSubstitutions) {
super(message);

this.substitutions = substitutions;
}
}

export class SubAccountLimitExceededError extends Error {}

export class NameTooLongError extends AccountTranslateError {
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,9 @@
"unknown_unlink": "Error unlinking canister.",
"get_exchange_rate": "Error getting the exchange rate of ICP to Cycles."
},
"error__imported_tokens": {
"too_many": "The limit of imported tokens that can be added has been reached (max. $limit)."
},
"error__sns": {
"undefined_project": "The requested project is invalid or throws an error.",
"list_summaries": "There was an unexpected error while loading the summaries of all deployed projects.",
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/lib/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,10 @@ interface I18nError__canister {
get_exchange_rate: string;
}

interface I18nError__imported_tokens {
too_many: string;
}

interface I18nError__sns {
undefined_project: string;
list_summaries: string;
Expand Down Expand Up @@ -1347,6 +1351,7 @@ interface I18n {
error__attach_wallet: I18nError__attach_wallet;
error__account: I18nError__account;
error__canister: I18nError__canister;
error__imported_tokens: I18nError__imported_tokens;
error__sns: I18nError__sns;
auth_accounts: I18nAuth_accounts;
auth_neurons: I18nAuth_neurons;
Expand Down
22 changes: 19 additions & 3 deletions frontend/src/tests/lib/canisters/nns-dapp.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ProposalPayloadNotFoundError,
ProposalPayloadTooLargeError,
SubAccountLimitExceededError,
TooManyImportedTokensError,
UnknownProposalPayloadError,
} from "$lib/canisters/nns-dapp/nns-dapp.errors";
import type { NNSDappService } from "$lib/canisters/nns-dapp/nns-dapp.idl";
Expand Down Expand Up @@ -492,9 +493,6 @@ describe("NNSDapp", () => {
await nnsDapp.getImportedTokens({ certified: true });

expect(service.get_imported_tokens).toBeCalledTimes(1);
expect(service.get_imported_tokens).toBeCalledWith({
certified: true,
});
});

it("should return imported tokens", async () => {
Expand Down Expand Up @@ -577,6 +575,24 @@ describe("NNSDapp", () => {
await expect(call).rejects.toThrow(AccountNotFoundError);
});

it("throws error if the limit has been reached", async () => {
const response: SetImportedTokensResponse = {
TooManyImportedTokens: { limit: 10 },
};
const service = mock<NNSDappService>();
service.set_imported_tokens.mockResolvedValue(response);

const nnsDapp = await createNnsDapp(service);

const call = async () => nnsDapp.setImportedTokens([]);

await expect(call).rejects.toThrowError(
new TooManyImportedTokensError("error__imported_tokens.too_many", {
limit: "10",
})
);
});

it("should provide generic error message", async () => {
const response = {
UnexpectedError: "message",
Expand Down

0 comments on commit e5d7a96

Please sign in to comment.