Skip to content

[SDK] Improve error handling in PayEmbed #7119

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

Merged
merged 1 commit into from
May 22, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/blue-comics-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Better error messages in PayEmbed
19 changes: 13 additions & 6 deletions packages/thirdweb/src/bridge/Buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { stringify } from "../utils/json.js";
import { ApiError } from "./types/Errors.js";
import type { PreparedQuote, Quote } from "./types/Quote.js";

/**
Expand Down Expand Up @@ -127,9 +128,12 @@ export async function quote(options: quote.Options): Promise<quote.Result> {
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code} | ${errorJson.message} - ${errorJson.correlationId}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: Quote } = await response.json();
Expand Down Expand Up @@ -357,9 +361,12 @@ export async function prepare(
});
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code} | ${errorJson.message} - ${errorJson.correlationId}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: PreparedQuote } = await response.json();
Expand Down
8 changes: 7 additions & 1 deletion packages/thirdweb/src/bridge/Chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import type { Chain } from "./types/Chain.js";
import { ApiError } from "./types/Errors.js";

/**
* Retrieves supported Universal Bridge chains.
Expand Down Expand Up @@ -59,7 +60,12 @@
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new Error(`${errorJson.code} | ${errorJson.message}`);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});

Check warning on line 68 in packages/thirdweb/src/bridge/Chains.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/bridge/Chains.ts#L63-L68

Added lines #L63 - L68 were not covered by tests
}

const { data }: { data: Chain[] } = await response.json();
Expand Down
10 changes: 7 additions & 3 deletions packages/thirdweb/src/bridge/Onramp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { stringify } from "../utils/json.js";
import { ApiError } from "./types/Errors.js";
import type { RouteStep } from "./types/Route.js";
import type { Token } from "./types/Token.js";

Expand Down Expand Up @@ -191,9 +192,12 @@ export async function prepare(

if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code || response.status} | ${errorJson.message || response.statusText} - ${errorJson.correlationId || "N/A"}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: OnrampPrepareQuoteResponseData } =
Expand Down
10 changes: 7 additions & 3 deletions packages/thirdweb/src/bridge/OnrampStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { ApiError } from "./types/Errors.js";

/**
* Retrieves the status of an Onramp session created via {@link Bridge.Onramp.prepare}. The
Expand Down Expand Up @@ -72,9 +73,12 @@
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code || response.status} | ${errorJson.message || response.statusText} - ${errorJson.correlationId || "N/A"}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});

Check warning on line 81 in packages/thirdweb/src/bridge/OnrampStatus.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/bridge/OnrampStatus.ts#L76-L81

Added lines #L76 - L81 were not covered by tests
}

const { data }: { data: status.Result } = await response.json();
Expand Down
2 changes: 1 addition & 1 deletion packages/thirdweb/src/bridge/Routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.routes", () => {
offset: 1000,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: InvalidRoutesRequest | The provided request is invalid.]`,
"[Error: The provided request is invalid.]",
);
});
});
8 changes: 7 additions & 1 deletion packages/thirdweb/src/bridge/Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Address as ox__Address, Hex as ox__Hex } from "ox";
import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { ApiError } from "./types/Errors.js";
import type { Route } from "./types/Route.js";

/**
Expand Down Expand Up @@ -162,7 +163,12 @@ export async function routes(options: routes.Options): Promise<routes.Result> {
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new Error(`${errorJson.code} | ${errorJson.message}`);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: Route[] } = await response.json();
Expand Down
19 changes: 13 additions & 6 deletions packages/thirdweb/src/bridge/Sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { stringify } from "../utils/json.js";
import { ApiError } from "./types/Errors.js";
import type { PreparedQuote, Quote } from "./types/Quote.js";

/**
Expand Down Expand Up @@ -126,9 +127,12 @@ export async function quote(options: quote.Options): Promise<quote.Result> {
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code} | ${errorJson.message} - ${errorJson.correlationId}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: Quote } = await response.json();
Expand Down Expand Up @@ -348,9 +352,12 @@ export async function prepare(
});
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code} | ${errorJson.message} - ${errorJson.correlationId}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: PreparedQuote } = await response.json();
Expand Down
10 changes: 7 additions & 3 deletions packages/thirdweb/src/bridge/Status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { ApiError } from "./types/Errors.js";
import type { Status } from "./types/Status.js";

/**
Expand Down Expand Up @@ -115,9 +116,12 @@
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code} | ${errorJson.message} - ${errorJson.correlationId}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});

Check warning on line 124 in packages/thirdweb/src/bridge/Status.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/bridge/Status.ts#L119-L124

Added lines #L119 - L124 were not covered by tests
}

const { data }: { data: Status } = await response.json();
Expand Down
10 changes: 7 additions & 3 deletions packages/thirdweb/src/bridge/Transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { stringify } from "../utils/json.js";
import { ApiError } from "./types/Errors.js";
import type { PreparedQuote } from "./types/Quote.js";

/**
Expand Down Expand Up @@ -212,9 +213,12 @@ export async function prepare(
});
if (!response.ok) {
const errorJson = await response.json();
throw new Error(
`${errorJson.code} | ${errorJson.message} - ${errorJson.correlationId}`,
);
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
message: errorJson.message || response.statusText,
correlationId: errorJson.correlationId || undefined,
statusCode: response.status,
});
}

const { data }: { data: PreparedQuote } = await response.json();
Expand Down
3 changes: 2 additions & 1 deletion packages/thirdweb/src/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export type {
} from "./types/Route.js";
export type { Status } from "./types/Status.js";
export type { Token } from "./types/Token.js";
export type { BridgeAction } from "./types/BridgeAction.js";
export type { Action } from "./types/BridgeAction.js";
export type { ApiError } from "./types/Errors.js";
2 changes: 1 addition & 1 deletion packages/thirdweb/src/bridge/types/BridgeAction.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type BridgeAction = "approval" | "transfer" | "buy" | "sell";
export type Action = "approval" | "transfer" | "buy" | "sell";
24 changes: 24 additions & 0 deletions packages/thirdweb/src/bridge/types/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type ErrorCode =
| "INVALID_INPUT"
| "ROUTE_NOT_FOUND"
| "AMOUNT_TOO_LOW"
| "AMOUNT_TOO_HIGH"
| "UNKNOWN_ERROR";

export class ApiError extends Error {
code: ErrorCode;
correlationId?: string;
statusCode: number;

constructor(args: {
code: ErrorCode;
message: string;
statusCode: number;
correlationId?: string;
}) {
super(args.message);
this.code = args.code;
this.correlationId = args.correlationId;
this.statusCode = args.statusCode;
}
}
4 changes: 2 additions & 2 deletions packages/thirdweb/src/bridge/types/Route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Hex as ox__Hex } from "ox";
import type { Chain } from "../../chains/types.js";
import type { ThirdwebClient } from "../../client/client.js";
import type { BridgeAction } from "./BridgeAction.js";
import type { Action } from "./BridgeAction.js";
import type { Token } from "./Token.js";

export type Route = {
Expand Down Expand Up @@ -35,7 +35,7 @@ export type RouteTransaction = {
/**
* The action this transaction performs. This can be "approval", "transfer", "buy", or "sell".
*/
action: BridgeAction;
action: Action;
/**
* The transaction ID, used for tracking purposes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
{props.value
? `${props.currency.symbol}${formatNumber(
Number(props.value),
6,
2,

Check warning on line 88 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/PayWIthCreditCard.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/PayWIthCreditCard.tsx#L88

Added line #L88 was not covered by tests
)}`
: "--"}
</Text>
Expand Down
Loading
Loading