Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This updates the gcs error types to leverage go 1.13 errors.Is/As functionality as well as confirm to the error infrastructure best practices.
- Loading branch information
Showing
5 changed files
with
109 additions
and
98 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
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 |
---|---|---|
@@ -1,74 +1,56 @@ | ||
// Copyright (c) 2019 The Decred developers | ||
// Copyright (c) 2020 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package gcs | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ErrorCode identifies a kind of error. | ||
type ErrorCode int | ||
// ErrorKind identifies a kind of error. It has full support for errors.Is and | ||
// errors.As, so the caller can directly check against an error kind when | ||
// determining the reason for an error. | ||
type ErrorKind string | ||
|
||
// These constants are used to identify a specific RuleError. | ||
const ( | ||
// ErrNTooBig signifies that the filter can't handle N items. | ||
ErrNTooBig ErrorCode = iota | ||
ErrNTooBig = ErrorKind("ErrNTooBig") | ||
|
||
// ErrPTooBig signifies that the filter can't handle `1/2**P` | ||
// collision probability. | ||
ErrPTooBig | ||
ErrPTooBig = ErrorKind("ErrPTooBig") | ||
|
||
// ErrBTooBig signifies that the provided Golomb coding bin size is larger | ||
// than the maximum allowed value. | ||
ErrBTooBig | ||
ErrBTooBig = ErrorKind("ErrBTooBig") | ||
|
||
// ErrMisserialized signifies a filter was misserialized and is missing the | ||
// N and/or P parameters of a serialized filter. | ||
ErrMisserialized | ||
|
||
// numErrorCodes is the maximum error code number used in tests. | ||
numErrorCodes | ||
ErrMisserialized = ErrorKind("ErrMisserialized") | ||
) | ||
|
||
// Map of ErrorCode values back to their constant names for pretty printing. | ||
var errorCodeStrings = map[ErrorCode]string{ | ||
ErrNTooBig: "ErrNTooBig", | ||
ErrPTooBig: "ErrPTooBig", | ||
ErrBTooBig: "ErrBTooBig", | ||
ErrMisserialized: "ErrMisserialized", | ||
} | ||
|
||
// String returns the ErrorCode as a human-readable name. | ||
func (e ErrorCode) String() string { | ||
if s := errorCodeStrings[e]; s != "" { | ||
return s | ||
} | ||
return fmt.Sprintf("Unknown ErrorCode (%d)", int(e)) | ||
// Error satisfies the error interface and prints human-readable errors. | ||
func (e ErrorKind) Error() string { | ||
return string(e) | ||
} | ||
|
||
// Error identifies a filter-related error. The caller can use type assertions | ||
// to access the ErrorCode field to ascertain the specific reason for the | ||
// failure. | ||
// Error identifies an error related to gcs filters. It has full support for | ||
// errors.Is and errors.As, so the caller can ascertain the specific reason | ||
// for the error by checking the underlying error. | ||
type Error struct { | ||
ErrorCode ErrorCode // Describes the kind of error | ||
Description string // Human readable description of the issue | ||
Err error | ||
Description string | ||
} | ||
|
||
// Error satisfies the error interface and prints human-readable errors. | ||
func (e Error) Error() string { | ||
return e.Description | ||
} | ||
|
||
// makeError creates an Error given a set of arguments. The error code must | ||
// be one of the error codes provided by this package. | ||
func makeError(c ErrorCode, desc string) Error { | ||
return Error{ErrorCode: c, Description: desc} | ||
// Unwrap returns the underlying wrapped error. | ||
func (e Error) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
// IsErrorCode returns whether err is an Error with a matching error code. | ||
func IsErrorCode(err error, c ErrorCode) bool { | ||
e, ok := err.(Error) | ||
return ok && e.ErrorCode == c | ||
// makeError creates an Error given a set of arguments. | ||
func makeError(kind ErrorKind, desc string) Error { | ||
return Error{Err: kind, Description: desc} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/decred/dcrd/gcs/v3 | ||
|
||
go 1.11 | ||
go 1.13 | ||
|
||
require ( | ||
github.com/dchest/siphash v1.2.1 | ||
|