Skip to content

fix: replace generic Error with typed AlexSDKError and fix fetchData bugs - #32

Open
vwinee21 wants to merge 1 commit into
alexgo-io:mainfrom
vwinee21:fix/typed-errors-and-fetch-improvements
Open

fix: replace generic Error with typed AlexSDKError and fix fetchData bugs#32
vwinee21 wants to merge 1 commit into
alexgo-io:mainfrom
vwinee21:fix/typed-errors-and-fetch-improvements

Conversation

@vwinee21

Copy link
Copy Markdown

Summary

Replace all throw new Error(...) across the SDK with a typed AlexSDKError class, and fix two bugs in fetchData.ts.

Changes

src/errors.ts (new)

  • AlexSDKError class with type, title, status, detail fields — inspired by RFC 9457 Problem Details
  • AlexErrorType const enum: FetchFailed, RouteNotFound, TooManyPools, TokenMappingNotFound

src/utils/fetchData.ts — bug fixes

  • getPrices() was throwing 'Failed to fetch token mappings' — correct message is 'Failed to fetch token prices'
  • getPrices() used any for response shape — replaced with typed RawPriceEntry / RawPricesResponse
  • HTTP status code from upstream now included in error detail

All helpers + alexSDK.ts, postConditions.ts, utils.ts

  • All throw new Error(...) replaced with typed AlexSDKError

src/index.ts

  • Exports AlexSDKError and AlexErrorType for consumers

Why

Generic throw new Error() forces consumers to parse message strings to distinguish error types. With AlexSDKError:

try {
  await sdk.getRate(Currency.STX, Currency.ALEX, 1_000_000n)
} catch (e) {
  if (e instanceof AlexSDKError && e.type === AlexErrorType.RouteNotFound) {
    // handle gracefully
  }
}

…bugs

Add AlexSDKError class (RFC 9457-inspired) with machine-readable type
URI, title, HTTP-equivalent status, and detail string. Replace all
throw new Error(...) with typed AlexSDKError instances.

Also fix two bugs in fetchData.ts:
- getPrices() reported 'Failed to fetch token mappings' instead of
  'Failed to fetch token prices' (wrong message, wrong function)
- getPrices() used any for response shape — replaced with typed
  RawPriceEntry / RawPricesResponse
- HTTP status codes from upstream now surfaced in error detail

Consumers can now instanceof AlexSDKError and branch on error.type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant