test: add unit tests for OpError type#43
Open
Onyx2406 wants to merge 2 commits intointerledger:mainfrom
Open
Conversation
Replace generic fmt.Errorf strings with a structured OpError type
that preserves the HTTP status code, status text, and any JSON
error details from the response body.
This enables callers to programmatically handle different error
scenarios:
err := client.IncomingPayment.Get(ctx, params)
var opErr *openpayments.OpError
if errors.As(err, &opErr) {
if opErr.IsNotFound() {
// handle 404
} else if opErr.IsUnauthorized() {
// handle 401 — token expired?
}
fmt.Println(opErr.Details) // server error details
}
Applied to: incoming payments (get, list, create, complete,
get public), outgoing payments (get, list, create), quotes
(get, create), and wallet address (get, get keys, get DID).
Grant errors are left unchanged as they already include
request/response body context.
Closes interledger#15
Add comprehensive tests covering: - Basic status code checks (IsNotFound, IsUnauthorized, IsForbidden) - JSON error body parsing into Details map - Description/message field extraction from JSON body - Plain text body handling - errors.As compatibility for type switching - IsStatus for arbitrary status codes (e.g., 429)
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.
Summary
Adds 5 unit tests for the
OpErrorstructured error type introduced in #39:TestOpErrorBasicTestOpErrorWithJSONBodyTestOpErrorWithPlainTextBodyTestOpErrorImplementsErrorerrors.Ascompatibility for type switchingTestOpErrorIsStatusDepends on: #39
Test plan
go test . -v -run TestOpError— all 5 pass