Skip to content

Commit

Permalink
Add more meaningful API error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Jan 31, 2024
1 parent 6e27759 commit c104ec7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
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}`,
);
}

0 comments on commit c104ec7

Please sign in to comment.