From 8a540998b922600669774fe38c55205914ae79a5 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 20 Nov 2023 14:59:12 -0300 Subject: [PATCH] nip11 fetch better errors. --- nip11/fetch.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/nip11/fetch.go b/nip11/fetch.go index a79159b..624139c 100644 --- a/nip11/fetch.go +++ b/nip11/fetch.go @@ -25,7 +25,7 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e } p, err := url.Parse(u) if err != nil { - return nil, fmt.Errorf("Cannot parse url: %s", u) + return nil, fmt.Errorf("cannot parse url: %s", u) } if p.Scheme == "ws" { p.Scheme = "http" @@ -42,11 +42,14 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e // send the request resp, err := http.DefaultClient.Do(req) if err != nil { - return nil, err + return nil, fmt.Errorf("request failed: %w", err) } defer resp.Body.Close() + info = &RelayInformationDocument{} - dec := json.NewDecoder(resp.Body) - err = dec.Decode(info) - return info, err + if err := json.NewDecoder(resp.Body).Decode(info); err != nil { + return nil, fmt.Errorf("invalid json: %w", err) + } + + return info, nil }