Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dex: general purpose go 1.13 error type #484

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"decred.org/dcrdex/dex/config"
)

// CoinNotFoundError is returned when a coin cannot be found, either because
// it has been spent or it never existed. This error may be returned from
// AuditContract, Refund or Redeem as those methods expect the provided coin
// to exist and be unspent.
const CoinNotFoundError = dex.Error("coin not found")
// CoinNotFoundError is returned when a coin cannot be found, either because it
// has been spent or it never existed. This error may be returned from
// AuditContract, Refund or Redeem as those methods expect the provided coin to
// exist and be unspent.
const CoinNotFoundError = dex.ErrorKind("coin not found")

// WalletInfo is auxiliary information about an ExchangeWallet.
type WalletInfo struct {
Expand Down
11 changes: 4 additions & 7 deletions dex/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import (
"time"
)

type Error string

func (err Error) Error() string { return string(err) }

const (
UnsupportedScriptError = Error("unsupported script type")
defaultLockTimeTaker = 24 * time.Hour
defaultlockTimeMaker = 48 * time.Hour
UnsupportedScriptError = ErrorKind("unsupported script type")

defaultLockTimeTaker = 24 * time.Hour
defaultlockTimeMaker = 48 * time.Hour
)

var (
Expand Down
39 changes: 39 additions & 0 deletions dex/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.

package dex
chappjc marked this conversation as resolved.
Show resolved Hide resolved

// ErrorKind identifies a kind of error that can be used to define new errors
// via const SomeError = dex.ErrorKind("something").
type ErrorKind string

// Error satisfies the error interface and prints human-readable errors.
func (e ErrorKind) Error() string {
return string(e)
}

// Error pairs an error with details.
type Error struct {
wrapped error
detail string
}

// Error satisfies the error interface, combining the wrapped error message with
// the details.
func (e Error) Error() string {
return e.wrapped.Error() + ": " + e.detail
}

// Unwrap returns the wrapped error, allowing errors.Is and errors.As to work.
func (e Error) Unwrap() error {
return e.wrapped
}

// NewError wraps the provided Error with details in a Error, facilitating the
// use of errors.Is and errors.As via errors.Unwrap.
func NewError(err error, detail string) Error {
return Error{
wrapped: err,
detail: detail,
}
}
45 changes: 45 additions & 0 deletions dex/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dex

import (
"errors"
"testing"
)

type testErr string

func (te testErr) Error() string {
return string(te)
}

const (
err0 = testErr("other error")
err1 = ErrorKind("error 1")
err2 = ErrorKind("error 2")
)
Comment on lines +8 to +18
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Illustrating the purpose of ErrorKind (to avoid the boiler plate of creating a type that defines Error() string), while allowing a const, which errors.New prevents.


func TestDexError(t *testing.T) {

dexErr := NewError(err1, "stuff")

var err1Back ErrorKind
if !errors.As(dexErr, &err1Back) {
t.Errorf("it wasn't an ErrorKind")
}
wantErrStr := err1.Error() + ": " + "stuff"
if dexErr.Error() != wantErrStr {
t.Errorf("incorrect error string %v, want %v", dexErr.Error(), wantErrStr)
}

if !errors.Is(dexErr, err1) {
t.Errorf("it wasn't err1")
}
if errors.Is(dexErr, err2) {
t.Errorf("it was err2")
}

otherErr := NewError(err0, "other stuff")
var err0Back testErr
if !errors.As(otherErr, &err0Back) {
t.Errorf("it wasn't an testErr")
}
}
2 changes: 1 addition & 1 deletion server/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var (
const (
assetName = "btc"
btcToSatoshi = 1e8
immatureTransactionError = dex.Error("immature output")
immatureTransactionError = dex.ErrorKind("immature output")
)

// btcNode represents a blockchain information fetcher. In practice, it is
Expand Down
2 changes: 1 addition & 1 deletion server/asset/btc/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/btcsuite/btcutil"
)

const ErrReorgDetected = dex.Error("reorg detected")
const ErrReorgDetected = dex.ErrorKind("reorg detected")

// TXIO is common information stored with an Input or Output.
type TXIO struct {
Expand Down
2 changes: 1 addition & 1 deletion server/asset/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// CoinNotFoundError is to be returned from Contract, Redemption, and
// FundingCoin when the specified transaction cannot be found. Used by the
// server to handle network latency.
const CoinNotFoundError = dex.Error("coin not found")
const CoinNotFoundError = dex.ErrorKind("coin not found")

// The Backend interface is an interface for a blockchain backend.
type Backend interface {
Expand Down
4 changes: 1 addition & 3 deletions server/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ var (
blockPollInterval = time.Second
)

type Error = dex.Error

const (
assetName = "dcr"
immatureTransactionError = Error("immature output")
immatureTransactionError = dex.ErrorKind("immature output")
)

// dcrNode represents a blockchain information fetcher. In practice, it is
Expand Down
2 changes: 1 addition & 1 deletion server/asset/dcr/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/decred/dcrd/dcrutil/v2"
)

const ErrReorgDetected = dex.Error("reorg detected")
const ErrReorgDetected = dex.ErrorKind("reorg detected")

// TXIO is common information stored with an Input or Output.
type TXIO struct {
Expand Down