Skip to content

Commit

Permalink
Merge pull request #173 from ipfs/fix/nice-errors
Browse files Browse the repository at this point in the history
http: cleanup http related errors
  • Loading branch information
Stebalien authored Aug 22, 2019
2 parents a9ee130 + aa35dd1 commit ba86293
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En

res, err := c.send(req)
if err != nil {
if isConnRefused(err) {
// Unwrap any URL errors. We don't really need to expose the
// underlying HTTP nonsense to the user.
if urlerr, ok := err.(*url.Error); ok {
err = urlerr.Err
}

if netoperr, ok := err.(*net.OpError); ok && netoperr.Op == "dial" {
// Connection refused.
if c.fallback != nil {
// XXX: this runs the PreRun twice
return c.fallback.Execute(req, re, env)
Expand Down Expand Up @@ -237,17 +244,3 @@ func getQuery(req *cmds.Request) (string, error) {

return query.Encode(), nil
}

func isConnRefused(err error) bool {
// unwrap url errors from http calls
if urlerr, ok := err.(*url.Error); ok {
err = urlerr.Err
}

netoperr, ok := err.(*net.OpError)
if !ok {
return false
}

return netoperr.Op == "dial"
}

0 comments on commit ba86293

Please sign in to comment.