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

code: remove the code registration hooks #91

Merged
merged 2 commits into from
Mar 14, 2023
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
22 changes: 3 additions & 19 deletions code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type ErrCoder interface {
// It also satisfies the ErrCoder interface, allowing the code to be recovered.
type codeError Code

// Error satisfies the error interface using the registered string for the
// code, if one is defined, or else a placeholder that describes the value.
// Error satisfies the error interface using the built-in string for the code,
// if one is defined, or else a placeholder that describes the value.
func (c codeError) Error() string { return Code(c).String() }

// ErrCode trivially satisfies the ErrCoder interface.
Expand All @@ -51,7 +51,7 @@ func (c codeError) Is(err error) bool {

// Err converts c to an error value, which is nil for code.NoError and
// otherwise an error value whose code is c and whose text is based on the
// registered string for c if one exists.
// built-in string for c if one exists.
func (c Code) Err() error {
if c == NoError {
return nil
Expand Down Expand Up @@ -88,22 +88,6 @@ var stdError = map[Code]string{
DeadlineExceeded: "deadline exceeded",
}

// Register adds a new Code value with the specified message string. This
// function will panic if the proposed value is already registered with a
// different string.
//
// Registering a code allows you to control the string returned by the String
// method for the code value you specify. It is not necessary to register a
// code before using it. An unregistered code renders a generic string.
func Register(value int32, message string) Code {
code := Code(value)
if s, ok := stdError[code]; ok && s != message {
panic(fmt.Sprintf("code %d is already registered for %q", code, s))
}
stdError[code] = message
return code
}

// FromError returns a Code to categorize the specified error.
// If err == nil, it returns code.NoError.
// If err is (or wraps) an ErrCoder, it returns the reported code value.
Expand Down
25 changes: 1 addition & 24 deletions code/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@ import (
"github.com/creachadair/jrpc2/code"
)

func TestRegistration(t *testing.T) {
const message = "fun for the whole family"
c := code.Register(-100, message)
if got := c.String(); got != message {
t.Errorf("Register(-100): got %q, want %q", got, message)
} else if c != -100 {
t.Errorf("Register(-100): got %d instead", c)
}
}

func TestRegistrationError(t *testing.T) {
defer func() {
if v := recover(); v != nil {
t.Logf("Register correctly panicked: %v", v)
} else {
t.Fatalf("Register should have panicked on input %d, but did not", code.ParseError)
}
}()
code.Register(int32(code.ParseError), "bogus")
}

type testCoder code.Code

func (t testCoder) ErrCode() code.Code { return code.Code(t) }
Expand Down Expand Up @@ -91,12 +70,10 @@ func TestErr(t *testing.T) {
code code.Code
want error
}
code.Register(1, "look for the bear necessities")
code.Register(2, "the simple bear necessities")
tests := []test{
{code.NoError, nil},
{0, errors.New("error code 0")},
{1, errors.New("look for the bear necessities")},
{1, errors.New("error code 1")},
{-17, errors.New("error code -17")},
}

Expand Down