-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror-type.go
58 lines (50 loc) · 1.7 KB
/
error-type.go
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
package prql
import "fmt"
// ErrorType is a Go style enum (internally a byte) for the type of error
// that occured. Intented to be used within the Error object to
// differentiate between different types, or sources, of errors.
type ErrorType byte
const (
// ErrorTypeUnknown signifies the type of error is unknown, and as such
// should represent a fatal error. The most common explanation for the type
// being unknown would be an improperly instantiated Error object, or
// an unknown runtime panic that was caught be recover().
//
// Encoded as "UNKNOWN"
ErrorTypeUnknown ErrorType = iota
// ErrorTypeSyntax signifies the error was generated during parsing, and
// is considered a syntax error (client error) relating to the input PRQL
// query.
//
// Encoded as "SYNTAX"
ErrorTypeSyntax
)
// String returns a string representation of the ErrorType enum. By default, Go
// will use this for encoding as well.
func (t ErrorType) String() string {
switch t {
case ErrorTypeSyntax:
return "SYNTAX"
}
return "UNKNOWN"
}
// Valid returns true if the ErrorType is within the acceptable range of
// known errors. This excludes the ErrorTypeUnknown constant, as those are
// reserved for truely uknown or zero-value errors.
func (t ErrorType) Valid() bool {
return t > ErrorTypeUnknown && t <= ErrorTypeSyntax
}
// UnmarshalText implements the encoding.TextUnmarshaler interface. Allows for
// deserialization to read string values and convert into the underlying
// ErrorType enum.
func (t *ErrorType) UnmarshalText(text []byte) error {
str := string(text)
switch str {
case "SYNTAX":
*t = ErrorTypeSyntax
default:
*t = ErrorTypeUnknown
return fmt.Errorf("ErrorType '%s' is invalid", str)
}
return nil
}