diff options
author | greatroar <61184462+greatroar@users.noreply.github.com> | 2021-10-03 15:58:57 +0200 |
---|---|---|
committer | Jakob Borg <jakob@kastelo.net> | 2021-10-22 18:40:40 +0200 |
commit | 8f8e8a92858ebb285fada3a09b568a04ec4cd132 (patch) | |
tree | 3b640d42c13ac6addda4bd47e8893d7fd6cbecd0 | |
parent | 46082f194c63bbf1ce79dac64a2ef56ef73ab5f2 (diff) |
lib/protocol: Simplify codeToError, errorToCode
Also be explicit about the fact that ErrNoError is nil. That name isn't
used anywhere outside this file.
-rw-r--r-- | lib/protocol/errors.go | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/lib/protocol/errors.go b/lib/protocol/errors.go index 1eebb54f99..07c30baaf4 100644 --- a/lib/protocol/errors.go +++ b/lib/protocol/errors.go @@ -2,43 +2,36 @@ package protocol -import ( - "errors" -) +import "errors" var ( - ErrNoError error ErrGeneric = errors.New("generic error") ErrNoSuchFile = errors.New("no such file") ErrInvalid = errors.New("file is invalid") ) -var lookupError = map[ErrorCode]error{ - ErrorCodeNoError: ErrNoError, - ErrorCodeGeneric: ErrGeneric, - ErrorCodeNoSuchFile: ErrNoSuchFile, - ErrorCodeInvalidFile: ErrInvalid, -} - -var lookupCode = map[error]ErrorCode{ - ErrNoError: ErrorCodeNoError, - ErrGeneric: ErrorCodeGeneric, - ErrNoSuchFile: ErrorCodeNoSuchFile, - ErrInvalid: ErrorCodeInvalidFile, -} - func codeToError(code ErrorCode) error { - err, ok := lookupError[code] - if !ok { + switch code { + case ErrorCodeNoError: + return nil + case ErrorCodeNoSuchFile: + return ErrNoSuchFile + case ErrorCodeInvalidFile: + return ErrInvalid + default: return ErrGeneric } - return err } func errorToCode(err error) ErrorCode { - code, ok := lookupCode[err] - if !ok { + switch err { + case nil: + return ErrorCodeNoError + case ErrNoSuchFile: + return ErrorCodeNoSuchFile + case ErrInvalid: + return ErrorCodeInvalidFile + default: return ErrorCodeGeneric } - return code } |