mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-14 00:07:01 +08:00
fix(hyperliquid): stop SDK init panic from crashing trader creation with 500
go-hyperliquid's NewExchange auto-fetches meta/spotMeta/perpDexs and panics if any of those API calls fail (NewInfo: panic(err)). The quick trade flow constructs a probe trader inside POST /api/traders, so any transient Hyperliquid API hiccup crashed the request with a recovered panic and a bare 500. Wrap the constructor in initExchangeClient, which converts the panic into an error that surfaces through the existing exchange-probe validation path as an honest, retryable message.
This commit is contained in:
37
trader/hyperliquid/client_init_test.go
Normal file
37
trader/hyperliquid/client_init_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package hyperliquid
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
hl "github.com/sonirico/go-hyperliquid"
|
||||
)
|
||||
|
||||
func TestInitExchangeClientConvertsPanicToError(t *testing.T) {
|
||||
// The SDK constructor panics when its automatic meta fetch fails
|
||||
// (go-hyperliquid info.go NewInfo: panic(err)). The wrapper must turn
|
||||
// that into an error instead of crashing the HTTP handler.
|
||||
_, err := initExchangeClient(func() *hl.Exchange {
|
||||
panic(errors.New("failed to fetch meta: API error 0: "))
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error when the SDK constructor panics, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "failed to fetch meta") {
|
||||
t.Errorf("error should carry the panic cause, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitExchangeClientPassesThroughSuccess(t *testing.T) {
|
||||
want := &hl.Exchange{}
|
||||
got, err := initExchangeClient(func() *hl.Exchange {
|
||||
return want
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatal("wrapper must return the constructed exchange unchanged")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user