Skip to content

Commit

Permalink
nip11 fetch better errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 20, 2023
1 parent f3c081f commit 8a54099
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions nip11/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

0 comments on commit 8a54099

Please sign in to comment.