chore: make error clearer (#1514)

This commit is contained in:
chenquan
2022-02-09 00:40:05 -06:00
committed by GitHub
parent 822ee2e1c5
commit 9c2c90e533
4 changed files with 18 additions and 5 deletions

View File

@@ -2,6 +2,13 @@ package discov
import "errors" import "errors"
var (
// errEmptyEtcdHosts indicates that etcd hosts are empty.
errEmptyEtcdHosts = errors.New("empty etcd hosts")
// errEmptyEtcdKey indicates that etcd key is empty.
errEmptyEtcdKey = errors.New("empty etcd key")
)
// EtcdConf is the config item with the given key on etcd. // EtcdConf is the config item with the given key on etcd.
type EtcdConf struct { type EtcdConf struct {
Hosts []string Hosts []string
@@ -27,9 +34,9 @@ func (c EtcdConf) HasTLS() bool {
// Validate validates c. // Validate validates c.
func (c EtcdConf) Validate() error { func (c EtcdConf) Validate() error {
if len(c.Hosts) == 0 { if len(c.Hosts) == 0 {
return errors.New("empty etcd hosts") return errEmptyEtcdHosts
} else if len(c.Key) == 0 { } else if len(c.Key) == 0 {
return errors.New("empty etcd key") return errEmptyEtcdKey
} else { } else {
return nil return nil
} }

View File

@@ -5,6 +5,9 @@ import (
"os" "os"
) )
// errExceedFileSize indicates that the file size is exceeded.
var errExceedFileSize = errors.New("exceed file size")
// A RangeReader is used to read a range of content from a file. // A RangeReader is used to read a range of content from a file.
type RangeReader struct { type RangeReader struct {
file *os.File file *os.File
@@ -29,7 +32,7 @@ func (rr *RangeReader) Read(p []byte) (n int, err error) {
} }
if rr.stop < rr.start || rr.start >= stat.Size() { if rr.stop < rr.start || rr.start >= stat.Size() {
return 0, errors.New("exceed file size") return 0, errExceedFileSize
} }
if rr.stop-rr.start < int64(len(p)) { if rr.stop-rr.start < int64(len(p)) {

View File

@@ -51,5 +51,5 @@ func unmarshalUseNumber(decoder *json.Decoder, v interface{}) error {
} }
func formatError(v string, err error) error { func formatError(v string, err error) error {
return fmt.Errorf("string: `%s`, error: `%s`", v, err.Error()) return fmt.Errorf("string: `%s`, error: `%w`", v, err)
} }

View File

@@ -7,6 +7,9 @@ import (
"github.com/zeromicro/go-zero/core/lang" "github.com/zeromicro/go-zero/core/lang"
) )
// errTimeout indicates a timeout.
var errTimeout = errors.New("timeout")
type ( type (
// Ticker interface wraps the Chan and Stop methods. // Ticker interface wraps the Chan and Stop methods.
Ticker interface { Ticker interface {
@@ -70,7 +73,7 @@ func (ft *fakeTicker) Tick() {
func (ft *fakeTicker) Wait(d time.Duration) error { func (ft *fakeTicker) Wait(d time.Duration) error {
select { select {
case <-time.After(d): case <-time.After(d):
return errors.New("timeout") return errTimeout
case <-ft.done: case <-ft.done:
return nil return nil
} }