|
|
@@ -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
|
|
|
}
|