-
Notifications
You must be signed in to change notification settings - Fork 6
/
errors.go
94 lines (78 loc) · 2.22 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
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
package validate
import (
"fmt"
"reflect"
)
// ErrorField is an error interface for field/value error.
type ErrorField interface {
error
FieldName() string
}
// errorField is a setter interface
type errorField interface {
setFieldName(string)
}
// ErrorValidation occurs when validator does not validate.
type ErrorValidation struct {
fieldName string
fieldValue reflect.Value
validatorType ValidatorType
validatorValue string
}
// FieldName gets a field name.
func (e ErrorValidation) FieldName() string {
return e.fieldName
}
// setFieldName sets a field name.
func (e *ErrorValidation) setFieldName(fieldName string) {
e.fieldName = fieldName
}
// Error returns an error.
func (e ErrorValidation) Error() string {
validator := string(e.validatorType)
if len(e.validatorValue) > 0 {
validator += "=" + e.validatorValue
}
if len(e.fieldName) > 0 {
return fmt.Sprintf("Validation error in field \"%v\" of type \"%v\" using validator \"%v\"", e.fieldName, e.fieldValue.Type(), validator)
}
return fmt.Sprintf("Validation error in value of type \"%v\" using validator \"%v\"", e.fieldValue.Type(), validator)
}
// ErrorSyntax occurs when there is a syntax error.
type ErrorSyntax struct {
fieldName string
expression string
near string
comment string
}
// FieldName gets a field name.
func (e ErrorSyntax) FieldName() string {
return e.fieldName
}
// setFieldName sets a field name.
func (e *ErrorSyntax) setFieldName(fieldName string) {
e.fieldName = fieldName
}
// Error returns an error.
func (e ErrorSyntax) Error() string {
if len(e.fieldName) > 0 {
return fmt.Sprintf("Syntax error when validating field \"%v\", expression \"%v\" near \"%v\": %v", e.fieldName, e.expression, e.near, e.comment)
}
return fmt.Sprintf("Syntax error when validating value, expression \"%v\" near \"%v\": %v", e.expression, e.near, e.comment)
}
// Set field name
func setFieldName(err ErrorField, fieldName string) ErrorField {
switch (err).(type) {
case ErrorValidation:
e := err.(ErrorValidation)
var i interface{} = &e
(i).(errorField).setFieldName(fieldName)
return e
case ErrorSyntax:
e := err.(ErrorSyntax)
var i interface{} = &e
(i).(errorField).setFieldName(fieldName)
return e
}
return err
}