-
Notifications
You must be signed in to change notification settings - Fork 44
feat: improve error messages for failed agent operations #78
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
Open
JerryIdoko
wants to merge
1
commit into
Stellar-Tools:main
Choose a base branch
from
JerryIdoko:feat/improve-error-messages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { decodeStellarError } from "../../../utils/errorDecoder"; | ||
|
|
||
| describe("errorDecoder", () => { | ||
| it("decodes Horizon error with result codes", () => { | ||
| const error = { | ||
| response: { | ||
| data: { | ||
| extras: { | ||
| result_codes: { | ||
| transaction: "tx_failed", | ||
| operations: ["op_underfunded"], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| const decoded = decodeStellarError(error); | ||
| expect(decoded).toContain("The transaction failed to execute."); | ||
| expect(decoded).toContain("Account has insufficient funds for this operation."); | ||
| }); | ||
|
|
||
| it("decodes Horizon error with multiple operation failures", () => { | ||
| const error = { | ||
| response: { | ||
| data: { | ||
| extras: { | ||
| result_codes: { | ||
| transaction: "tx_failed", | ||
| operations: ["op_success", "op_no_trust"], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| const decoded = decodeStellarError(error); | ||
| expect(decoded).toContain("The transaction failed to execute."); | ||
| expect(decoded).toContain("The account is missing a trustline for the required asset."); | ||
| expect(decoded).not.toContain("op_success"); | ||
| }); | ||
|
|
||
| it("handles Horizon detail message if no result codes", () => { | ||
| const error = { | ||
| response: { | ||
| data: { | ||
| detail: "The request was invalid.", | ||
| }, | ||
| }, | ||
| }; | ||
| const decoded = decodeStellarError(error); | ||
| expect(decoded).toBe("The request was invalid."); | ||
| }); | ||
|
|
||
| it("handles Soroban FAILED status", () => { | ||
| const error = { | ||
| status: "FAILED", | ||
| }; | ||
| const decoded = decodeStellarError(error); | ||
| expect(decoded).toBe("The smart contract transaction failed."); | ||
| }); | ||
|
|
||
| it("handles general error objects", () => { | ||
| const error = new Error("Something went wrong"); | ||
| const decoded = decodeStellarError(error); | ||
| expect(decoded).toBe("Something went wrong"); | ||
| }); | ||
|
|
||
| it("extracts codes from error messages", () => { | ||
| const error = new Error("Error: op_low_reserve"); | ||
| const decoded = decodeStellarError(error); | ||
| expect(decoded).toContain("Account does not have enough balance to meet the minimum reserve requirement."); | ||
| expect(decoded).toContain("(op_low_reserve)"); | ||
| }); | ||
|
|
||
| it("handles unknown errors", () => { | ||
| const decoded = decodeStellarError(null); | ||
| expect(decoded).toBe("Unknown error occurred."); | ||
|
|
||
| expect(decodeStellarError({})).toBe("[object Object]"); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * Utility to decode Stellar and Soroban error messages into human-readable strings. | ||
| */ | ||
|
|
||
| export interface StellarError { | ||
| response?: { | ||
| data?: { | ||
| extras?: { | ||
| result_codes?: { | ||
| transaction?: string; | ||
| operations?: string[]; | ||
| }; | ||
| }; | ||
| detail?: string; | ||
| }; | ||
| status?: number; | ||
| }; | ||
| message?: string; | ||
| } | ||
|
|
||
| const ERROR_MESSAGES: Record<string, string> = { | ||
| // Transaction level | ||
| tx_failed: "The transaction failed to execute.", | ||
| tx_bad_seq: "Transaction sequence number is incorrect. Please try again.", | ||
| tx_too_late: "The transaction has expired.", | ||
| tx_too_early: "The transaction was submitted too early.", | ||
| tx_insufficient_fee: "The transaction fee was too low.", | ||
| tx_missing_operation: "The transaction has no operations.", | ||
| tx_bad_auth: "Transaction has too few or invalid signatures.", | ||
| tx_no_source_account: "The source account was not found.", | ||
|
|
||
| // Operation level | ||
| op_underfunded: "Account has insufficient funds for this operation.", | ||
| op_low_reserve: "Account does not have enough balance to meet the minimum reserve requirement.", | ||
| op_no_trust: "The account is missing a trustline for the required asset.", | ||
| op_not_authorized: "The account is not authorized to hold this asset.", | ||
| op_line_full: "The trustline limit for this asset has been reached.", | ||
| op_no_destination: "The destination account does not exist.", | ||
| op_no_issuer: "The asset issuer does not exist.", | ||
| op_src_not_authorized: "The source account is not authorized to send this asset.", | ||
| op_src_no_trust: "The source account is missing a trustline for the asset.", | ||
| op_too_many_subentries: "The account has reached the maximum number of subentries (trustlines, offers, etc.).", | ||
| op_cross_self: "Cannot trade with oneself.", | ||
| op_bad_auth: "Operation has invalid signatures.", | ||
| op_immutable_set: "The account is immutable and its options cannot be changed.", | ||
|
|
||
| // DEX specific | ||
| op_under_dest_min: "The swap could not be completed at the requested price (slippage too high).", | ||
| op_over_source_max: "The swap would require more source assets than the specified maximum.", | ||
| op_no_path: "No viable path found for this swap.", | ||
|
|
||
| // Soroban/Smart Contract | ||
| HostError: "A smart contract execution error occurred.", | ||
| ContractError: "The smart contract returned an error.", | ||
| }; | ||
|
|
||
| /** | ||
| * Decodes a Stellar/Horizon/Soroban error into a human-readable message. | ||
| */ | ||
| export function decodeStellarError(error: any): string { | ||
| if (!error) return "Unknown error occurred."; | ||
|
|
||
| // Handle Horizon errors | ||
| const resultCodes = error.response?.data?.extras?.result_codes; | ||
| if (resultCodes) { | ||
| const messages: string[] = []; | ||
|
|
||
| // Check transaction-level error | ||
| if (resultCodes.transaction && ERROR_MESSAGES[resultCodes.transaction]) { | ||
| messages.push(ERROR_MESSAGES[resultCodes.transaction]); | ||
| } | ||
|
|
||
| // Check operation-level errors | ||
| if (resultCodes.operations && Array.isArray(resultCodes.operations)) { | ||
| resultCodes.operations.forEach((opCode: string) => { | ||
| if (opCode !== "op_success" && ERROR_MESSAGES[opCode]) { | ||
| messages.push(ERROR_MESSAGES[opCode]); | ||
| } else if (opCode !== "op_success") { | ||
| messages.push(`Operation failed: ${opCode}`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (messages.length > 0) { | ||
| return messages.join(" "); | ||
| } | ||
| } | ||
|
|
||
| // Handle Horizon detail message | ||
| if (error.response?.data?.detail) { | ||
| return error.response.data.detail; | ||
| } | ||
|
|
||
| // Handle Soroban RPC errors | ||
| if (error.status === "FAILED") { | ||
| return "The smart contract transaction failed."; | ||
| } | ||
|
|
||
| // Handle general Error objects | ||
| if (error.message) { | ||
| // Try to extract known error codes from message strings | ||
| for (const [code, msg] of Object.entries(ERROR_MESSAGES)) { | ||
| if (error.message.includes(code)) { | ||
| return `${msg} (${code})`; | ||
| } | ||
| } | ||
| return error.message; | ||
| } | ||
|
|
||
| return String(error); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Rewrapping every failure here discards the original error object, so callers lose the underlying type, stack, and SDK-specific fields.
Prompt for AI agents