refactor(server): replace try/catch with neverthrow Result types#742
Open
kira-autonoma wants to merge 3 commits intoMerit-Systems:masterfrom
Open
refactor(server): replace try/catch with neverthrow Result types#742kira-autonoma wants to merge 3 commits intoMerit-Systems:masterfrom
kira-autonoma wants to merge 3 commits intoMerit-Systems:masterfrom
Conversation
Remove `gemini-2.0-flash-preview-image-generation` and `gemini-2.5-flash-image-preview` from the SDK type definitions, model pricing list, and smoke test blacklist. Update templates to use the stable `gemini-2.5-flash-image` replacement. Closes Merit-Systems#634 Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Add typed error variants in errors/results.ts (DbError, AuthError, SettleError, RefundError, ResourceError) - Refactor settle() to return ResultAsync<SettleSuccess, SettleError> with fromThrowable for sync validateXPaymentHeader - Refactor refund() to return ResultAsync<void, RefundError> instead of swallowing errors - Refactor DbService.validateApiKey() to return ResultAsync<ApiKeyValidationResult, AuthError> - Refactor DbService.getBalance() to return ResultAsync<Balance, DbError> - Refactor DbService.createPaidTransaction() to return ResultAsync<Transaction, DbError> - Refactor DbService.createFreeTierTransaction() to return ResultAsync<..., DbError> - Update EchoControlService to handle ResultAsync returns from DbService - Update handlers.ts and resources/handler.ts to use .isOk()/.isErr()/.value on Result types - Add neverthrow ^7.2.0 dependency to echo-server package Closes Merit-Systems#581 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- Replace all try/catch in handleApiRequest, handle402Request, executeResourceWithRefund, and handleResourceRequestWithErrorHandling - Use ResultAsync chains with .andThen() and .match() throughout - Remove PaymentRequiredError from handler; 402 errors now carried as RESOURCE_PAYMENT_FAILED variant in ResourceError - Update ResourceError cause types to unknown for external rejections
Contributor
|
Someone is attempting to deploy a commit to the Merit Systems Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #581
Summary
Replaces all try/catch blocks in
packages/app/serverwith typed neverthrowResultandResultAsynctypes, providing exhaustive, compile-time error handling throughout the server.Changes
New:
src/errors/results.tsDiscriminated union error types for each layer:
DbError— database operation failures with structured contextAuthError— authentication/authorization failures with subtypesSettleError— x402 payment settlement failure variantsRefundError— refund transfer failuresResourceError— top-level handler errors composing the aboveRefactored:
handlers/settle.tsResultAsync<SettleSuccess, SettleError>instead ofPromise<... | undefined>fromThrowableto wrap the synchronousvalidateXPaymentHeader.andThen()chain: smart account → header parse → payload validate → amount check → facilitator settleres: Responsefrom signature — callers own the HTTP responseRefactored:
handlers/refund.tsResultAsync<void, RefundError>instead of swallowing errorsRefactored:
resources/handler.tshandleApiRequest→ResultAsync<TOutput, ResourceError>via chained.andThen()executeResourceWithRefund→ResultAsync<TOutput, ResourceError>; refund on error via.mapErr()handle402Request→ResultAsync<TOutput, ResourceError>composingsettleandexecuteResourceWithRefundhandleResourceRequestuses.match()at the HTTP boundary — no try/catchhandleResourceRequestWithErrorHandlingwraps inResultAsync.fromPromiseand.match()Refactored:
services/DbService.tsvalidateApiKey→ResultAsync<ApiKeyValidationResult, AuthError>getBalance→ResultAsync<Balance, DbError>createPaidTransaction→ResultAsync<Transaction, DbError>createFreeTierTransaction→ResultAsync<{transaction, userSpendPoolUsage}, DbError>Refactored:
services/EchoControlService.ts+FreeTierService.tsverifyApiKey,getBalance,createTransaction,createFreeTierTransaction,createPaidTransactionall propagateResultAsync.isErr()checks with typed error variantsDesign Decisions
fromThrowablewraps sync-throwing functions at the boundary; async operations useResultAsync.fromPromise.match()at HTTP boundary — Results never leak into Express response handlerscause: unknownon external-facing error variants where the underlying throw type cannot be statically knownswitchonerror.typeis idiomatic neverthrowTypeScript Status
Zero new type errors introduced. Pre-existing infrastructure errors (
generated/prismanot generated, workspace SDK not built) are unaffected and unchanged.