This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from libp2p/feat/improve-errors
dial: return a nice custom dial error
- Loading branch information
Showing
5 changed files
with
121 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package swarm | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
peer "github.com/libp2p/go-libp2p-peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// maxDialDialErrors is the maximum number of dial errors we record | ||
const maxDialDialErrors = 16 | ||
|
||
// DialError is the error type returned when dialing. | ||
type DialError struct { | ||
Peer peer.ID | ||
DialErrors []TransportError | ||
Cause error | ||
Skipped int | ||
} | ||
|
||
func (e *DialError) recordErr(addr ma.Multiaddr, err error) { | ||
if len(e.DialErrors) >= maxDialDialErrors { | ||
e.Skipped++ | ||
return | ||
} | ||
e.DialErrors = append(e.DialErrors, TransportError{ | ||
Address: addr, | ||
Cause: err, | ||
}) | ||
} | ||
|
||
func (e *DialError) Error() string { | ||
var builder strings.Builder | ||
fmt.Fprintf(&builder, "failed to dial %s:", e.Peer) | ||
if e.Cause != nil { | ||
fmt.Fprintf(&builder, " %s", e.Cause) | ||
} | ||
for _, te := range e.DialErrors { | ||
fmt.Fprintf(&builder, "\n * [%s] %s", te.Address, te.Cause) | ||
} | ||
if e.Skipped > 0 { | ||
fmt.Fprintf(&builder, "\n ... skipping %d errors ...", e.Skipped) | ||
} | ||
return builder.String() | ||
} | ||
|
||
// Unwrap implements https://godoc.org/golang.org/x/xerrors#Wrapper. | ||
func (e *DialError) Unwrap() error { | ||
// If we have a context error, that's the "ultimate" error. | ||
if e.Cause != nil { | ||
return e.Cause | ||
} | ||
return nil | ||
} | ||
|
||
var _ error = (*DialError)(nil) | ||
|
||
// TransportError is the error returned when dialing a specific address. | ||
type TransportError struct { | ||
Address ma.Multiaddr | ||
Cause error | ||
} | ||
|
||
func (e *TransportError) Error() string { | ||
return fmt.Sprintf("failed to dial %s: %s", e.Address, e.Cause) | ||
} | ||
|
||
var _ error = (*TransportError)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters