lib/protocol: Simplify codeToError, errorToCode
Also be explicit about the fact that ErrNoError is nil. That name isn't used anywhere outside this file.
This commit is contained in:
parent
46082f194c
commit
8f8e8a9285
|
@ -2,43 +2,36 @@
|
||||||
|
|
||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import "errors"
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNoError error
|
|
||||||
ErrGeneric = errors.New("generic error")
|
ErrGeneric = errors.New("generic error")
|
||||||
ErrNoSuchFile = errors.New("no such file")
|
ErrNoSuchFile = errors.New("no such file")
|
||||||
ErrInvalid = errors.New("file is invalid")
|
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 {
|
func codeToError(code ErrorCode) error {
|
||||||
err, ok := lookupError[code]
|
switch code {
|
||||||
if !ok {
|
case ErrorCodeNoError:
|
||||||
|
return nil
|
||||||
|
case ErrorCodeNoSuchFile:
|
||||||
|
return ErrNoSuchFile
|
||||||
|
case ErrorCodeInvalidFile:
|
||||||
|
return ErrInvalid
|
||||||
|
default:
|
||||||
return ErrGeneric
|
return ErrGeneric
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorToCode(err error) ErrorCode {
|
func errorToCode(err error) ErrorCode {
|
||||||
code, ok := lookupCode[err]
|
switch err {
|
||||||
if !ok {
|
case nil:
|
||||||
|
return ErrorCodeNoError
|
||||||
|
case ErrNoSuchFile:
|
||||||
|
return ErrorCodeNoSuchFile
|
||||||
|
case ErrInvalid:
|
||||||
|
return ErrorCodeInvalidFile
|
||||||
|
default:
|
||||||
return ErrorCodeGeneric
|
return ErrorCodeGeneric
|
||||||
}
|
}
|
||||||
return code
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue