Refactor phoenix client, fix bugs, remove unused imports#12
Conversation
- Extract shared HTTP client helper (phoenix/client.go) with loadPassword(), doRequest(), and doGet() to eliminate duplicated config loading, client creation, and basic auth setup across all 10 phoenix API files - Replace util.CheckAndPanic/log.Fatal with proper error returns in phoenix package for safer error handling - Fix missing fee_sats column in Db_select_card_payments() query — the CardPayment struct had a FeeSats field that was never populated - Fix early w.WriteHeader(200) in bcp_batch_create.go that sent a success status before validation, causing expired/missing program cards to return HTTP 200 with empty body instead of an error - Replace inline payment hash conversion in wallet_api_addinvoice.go with existing util.ConvertPaymentHash() helper - Remove unused _ "github.com/mattn/go-sqlite3" blank imports from 11 web handler/test files (driver is already registered via card/db import) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR improves the Phoenix API client code through refactoring and fixes several bugs. The main changes extract common HTTP client logic into shared helper functions, fix a database query that was missing a field, correct premature HTTP response writing, and remove unused imports.
Changes:
- Extract shared Phoenix HTTP client helper functions to eliminate ~260 lines of duplicated code across 10 API files
- Fix missing
fee_satsfield in card payment database query - Fix premature HTTP 200 response in batch card creation that prevented proper error handling
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| docker/card/web/web_test.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_wipecard.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_payinvoice.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_getuserinvoices.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_gettxs.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_getcardkeys.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_getcard.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_create.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_balance.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_auth.go | Remove unused sqlite3 import |
| docker/card/web/wallet_api_addinvoice.go | Replace inline payment hash conversion with util helper (introduces error handling regression) |
| docker/card/web/bcp_batch_create.go | Fix premature HTTP 200 response before validation |
| docker/card/phoenix/send_lightning_payment.go | Use shared client helpers and improve error handling |
| docker/card/phoenix/list_outgoing_payments.go | Refactor to use shared doRequest helper |
| docker/card/phoenix/list_incoming_payments.go | Refactor to use shared doRequest helper |
| docker/card/phoenix/list_channels.go | Refactor to use shared doGet helper |
| docker/card/phoenix/get_outgoing_payment.go | Refactor to use shared doGet helper |
| docker/card/phoenix/get_offer.go | Refactor to use shared doGet helper |
| docker/card/phoenix/get_node_info.go | Refactor to use shared doGet helper |
| docker/card/phoenix/get_incoming_payment.go | Refactor to use shared doGet helper |
| docker/card/phoenix/get_balance.go | Refactor to use shared doGet helper |
| docker/card/phoenix/create_invoice.go | Refactor to use shared doRequest helper with proper error handling |
| docker/card/phoenix/client.go | New file with shared HTTP client helpers (loadPassword, doRequest, doGet) |
| docker/card/db/db_select.go | Fix missing fee_sats field in card payments query |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for _, rHashByte := range rHashByteSlice { | ||
| rHashIntSlice = append(rHashIntSlice, int(rHashByte)) | ||
| } | ||
| rHashIntSlice := util.ConvertPaymentHash(createInvoiceResponse.PaymentHash) |
There was a problem hiding this comment.
This change removes explicit error handling that was present in the original code. The previous implementation checked for hex decoding errors and returned a proper HTTP error response. The util.ConvertPaymentHash function uses CheckAndPanic, which will cause a server panic instead of returning a user-friendly error.
The original code was:
rHashByteSlice, err := hex.DecodeString(createInvoiceResponse.PaymentHash)
if err != nil {
log.Error("hex decode error: ", err)
sendError(w, "Error", 999, "failed to decode payment hash")
return
}While invalid hex from the Phoenix API is unlikely, this represents a regression in error handling that could cause unexpected server crashes. Consider reverting to the explicit error handling or creating a non-panicking version of ConvertPaymentHash for use in web handlers.
| return sendLightningPaymentResponse, | ||
| "fail_status_code", | ||
| errors.New("fail status code returned for SendLightningPayment " + strconv.Itoa(res.StatusCode)) | ||
| errors.New("fail status code returned for SendLightningPayment") |
There was a problem hiding this comment.
The status code was removed from the error message. While the status code is still logged in the log.Warning on line 93, including it in the error message itself can be helpful for debugging when the error is propagated up the call stack and logged elsewhere.
The previous error message was more informative: "fail status code returned for SendLightningPayment 404" vs the new "fail status code returned for SendLightningPayment"
Consider retaining the status code in the error message using fmt.Errorf instead of errors.New to avoid needing the strconv import while still providing detailed error information.
Summary
phoenix/client.go) withloadPassword(),doRequest(), anddoGet()— eliminates duplicated config loading, HTTP client creation, and basic auth setup across all 10 phoenix API files (-260 lines)fee_satsinDb_select_card_payments()query — theCardPaymentstruct had aFeeSatsfield that was never populated from the databasew.WriteHeader(200)inbcp_batch_create.go— was sending HTTP 200 before validation, so expired/missing program cards returned success with empty body instead of an errorutil.CheckAndPanic/log.Fatalwith proper error returns in phoenix package for safer error handlingutil.ConvertPaymentHash()helper inwallet_api_addinvoice.goinstead of inline reimplementation_ "github.com/mattn/go-sqlite3"blank imports from 11 web handler/test files (driver already registered viacard/dbimport)Test plan
go vet ./...passesgo test -race -count=1 ./...passes (crypto, db, web)🤖 Generated with Claude Code