/
errors.go
47 lines (36 loc) · 1.05 KB
/
errors.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
package dberr
import "fmt"
type errorType string
const (
ErrorNil errorType = ""
ErrorUndefined errorType = "Unknown Error."
// IO error
ErrorIO errorType = "IO error has occured, see log for more details."
ErrorNoDoc errorType = "Document `%d` does not exist"
// Document errors
ErrorDocTooLarge errorType = "Document is too large. Max: `%d`, Given: `%d`"
// Query input errors
ErrorNeedIndex errorType = "Please index %v and retry query %v."
ErrorExpectingSubQuery errorType = "Expecting a vector of sub-queries, but %v given."
ErrorExpectingInt errorType = "Expecting `%s` as an integer, but %v given."
ErrorMissing errorType = "Missing `%s`"
)
func New(err errorType, details ...interface{}) Error {
return Error{err, details}
}
type Error struct {
err errorType
details []interface{}
}
func (e Error) Error() string {
return fmt.Sprintf(string(e.err), e.details...)
}
func Type(e error) errorType {
if e == nil {
return ErrorNil
}
if err, ok := e.(Error); ok {
return err.err
}
return ErrorUndefined
}