1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package gitaly
import (
"errors"
"fmt"
)
type ErrorCode int
const (
// UnknownError - what happened is unknown
UnknownError ErrorCode = iota
// NotFound - file/directory/ref was not found
NotFound
// FileTooBig - file is too big
FileTooBig
// RpcError - gRPC returned an error
RpcError
// ProtocolError - protocol violation, an unexpected situation occurred.
ProtocolError
// UnexpectedTreeEntryType - returned when TreeEntryResponse has an unexpected type.
UnexpectedTreeEntryType
)
func (e ErrorCode) String() string {
switch e {
case UnknownError:
return "UnknownErr"
case NotFound:
return "NotFound"
case FileTooBig:
return "FileTooBig"
case RpcError:
return "RpcError"
case ProtocolError:
return "ProtocolError"
case UnexpectedTreeEntryType:
return "UnexpectedTreeEntryType"
default:
return fmt.Sprintf("invalid ErrorCode: %d", e)
}
}
type Error struct {
Code ErrorCode
Cause error
Message string
// RpcName is the name of gRPC method that failed.
RpcName string
// Path contains name of the file or directory the operation was being carried on.
Path string
}
func NewNotFoundError(rpcName, path string) error {
return &Error{
Code: NotFound,
Message: "file/directory/ref not found",
RpcName: rpcName,
Path: path,
}
}
func NewFileTooBigError(err error, rpcName, path string) error {
return &Error{
Code: FileTooBig,
Cause: err,
Message: "file is too big",
RpcName: rpcName,
Path: path,
}
}
func NewUnexpectedTreeEntryTypeError(rpcName, path string) error {
return &Error{
Code: UnexpectedTreeEntryType,
Message: "file is not a usual file",
RpcName: rpcName,
Path: path,
}
}
func NewRpcError(err error, rpcName, path string) error {
return &Error{
Code: RpcError,
Cause: err,
Message: "RPC failed",
RpcName: rpcName,
Path: path,
}
}
func NewProtocolError(err error, message, rpcName, path string) error {
return &Error{
Code: ProtocolError,
Cause: err,
Message: message,
RpcName: rpcName,
Path: path,
}
}
func (e *Error) Error() string {
format := "%s"
args := []interface{}{e.Code}
if e.RpcName != "" {
format += ": %s"
args = append(args, e.RpcName)
}
format += ": %s"
args = append(args, e.Message)
if e.Path != "" {
format += ": %s"
args = append(args, e.Path)
}
if e.Cause != nil {
format += ": %v"
args = append(args, e.Cause)
}
return fmt.Sprintf(format, args...)
}
func (e *Error) Unwrap() error {
return e.Cause
}
func ErrorCodeFromError(err error) ErrorCode {
var e *Error
if !errors.As(err, &e) {
return UnknownError
}
return e.Code
}
|