Skip to content

Commit

Permalink
dex: DexError
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Jun 16, 2020
1 parent 8968f26 commit d7d18c9
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 14 deletions.
4 changes: 2 additions & 2 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

// CoinNotFoundError is returned from AuditContract when the counter-party's
// swap transaction coin cannot be found.
const CoinNotFoundError = dex.Error("coin not found")
const CoinNotFoundError = dex.ErrorKind("coin not found")

// CoinSpentError is returned when an attempt is made to treat a spent coin as
// unspent. This error may be returned from AuditContract, Refund and Redeem
// as those methods expect the provided coin to be unspent.
const CoinSpentError = dex.Error("coin has been spent")
const CoinSpentError = dex.ErrorKind("coin has been spent")

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

type Error string
type assetError string

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

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

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

var (
Expand Down
36 changes: 36 additions & 0 deletions dex/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dex

// 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")
)

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

0 comments on commit d7d18c9

Please sign in to comment.