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

Add more meaningful API error messages #271

Merged
merged 1 commit into from
Feb 1, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
# Unreleased

- Add optional `options` param to `getAccountEventsByCreationNumber` query for paginations and order by
- Add more meaningful API error messages

# 1.5.1 (2024-01-24)

Expand Down
18 changes: 18 additions & 0 deletions src/api/aptosConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,22 @@ export class AptosConfig {
isIndexerRequest(url: string): boolean {
return NetworkToIndexerAPI[this.network] === url;
}

/**
* Checks if the URL is a known fullnode endpoint
*
* @internal
* */
isFullnodeRequest(url: string): boolean {
return NetworkToNodeAPI[this.network] === url;
}

/**
* Checks if the URL is a known faucet endpoint
*
* @internal
* */
isFaucetRequest(url: string): boolean {
return NetworkToFaucetAPI[this.network] === url;
}
}
11 changes: 9 additions & 2 deletions src/client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export async function aptosRequest<Req extends {}, Res extends {}>(
throw new AptosApiError(
options,
result,
indexerResponse.errors[0].message ?? `Unhandled Error ${response.status} : ${response.statusText}`,
`Indexer error: ${indexerResponse.errors[0].message}` ??
`Indexer unhandled Error ${response.status} : ${response.statusText}`,
);
}
result.data = indexerResponse.data as Res;
Expand All @@ -112,5 +113,11 @@ export async function aptosRequest<Req extends {}, Res extends {}>(
errorMessage = `Unhandled Error ${result.status} : ${result.statusText}`;
}

throw new AptosApiError(options, result, errorMessage);
// Since we already checked if it is an Indexer request, here we can be sure
// it either Fullnode or Faucet request
throw new AptosApiError(
options,
result,
`${aptosConfig.isFullnodeRequest(url) ? "Fullnode" : "Faucet"} error: ${errorMessage}`,
);
}
Loading