Skip to content

test: add unit tests for OpError type#43

Open
Onyx2406 wants to merge 2 commits intointerledger:mainfrom
Onyx2406:test/operror-unit-tests
Open

test: add unit tests for OpError type#43
Onyx2406 wants to merge 2 commits intointerledger:mainfrom
Onyx2406:test/operror-unit-tests

Conversation

@Onyx2406
Copy link
Copy Markdown

@Onyx2406 Onyx2406 commented Apr 3, 2026

Summary

Adds 5 unit tests for the OpError structured error type introduced in #39:

Test What it verifies
TestOpErrorBasic Status code, IsNotFound/IsUnauthorized
TestOpErrorWithJSONBody JSON body parsing → Details map + message extraction
TestOpErrorWithPlainTextBody Plain text body included in message
TestOpErrorImplementsError errors.As compatibility for type switching
TestOpErrorIsStatus Custom status code matching (e.g., 429)

Depends on: #39

Test plan

  • go test . -v -run TestOpError — all 5 pass

Onyx2406 added 2 commits April 3, 2026 08:16
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)
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