syncthing/lib/protocol/errors.go

45 lines
862 B
Go
Raw Normal View History

2015-02-08 12:04:01 +01:00
// Copyright (C) 2014 The Protocol Authors.
package protocol
import (
"errors"
)
var (
2015-09-22 20:34:24 +02:00
ErrNoError error
ErrGeneric = errors.New("generic error")
ErrNoSuchFile = errors.New("no such file")
ErrInvalid = errors.New("file is invalid")
2015-02-08 12:04:01 +01:00
)
var lookupError = map[ErrorCode]error{
ErrorCodeNoError: ErrNoError,
ErrorCodeGeneric: ErrGeneric,
ErrorCodeNoSuchFile: ErrNoSuchFile,
ErrorCodeInvalidFile: ErrInvalid,
2015-02-08 12:04:01 +01:00
}
var lookupCode = map[error]ErrorCode{
ErrNoError: ErrorCodeNoError,
ErrGeneric: ErrorCodeGeneric,
ErrNoSuchFile: ErrorCodeNoSuchFile,
ErrInvalid: ErrorCodeInvalidFile,
2015-02-08 12:04:01 +01:00
}
func codeToError(code ErrorCode) error {
err, ok := lookupError[code]
2015-02-08 12:04:01 +01:00
if !ok {
return ErrGeneric
}
return err
}
func errorToCode(err error) ErrorCode {
2015-02-08 12:04:01 +01:00
code, ok := lookupCode[err]
if !ok {
return ErrorCodeGeneric
2015-02-08 12:04:01 +01:00
}
return code
}