/
errors.go
56 lines (44 loc) · 1.2 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
package api
import (
"bytes"
"fmt"
"github.com/go-playground/validator"
)
type ApiError struct {
Code int
Desc string
}
func NewApiError(code int, desc string) *ApiError {
return &ApiError{code, desc}
}
func (e *ApiError) Error() string {
return e.Desc
}
func BindError() *ApiError {
return &ApiError{Code: -2, Desc: "bind error"}
}
func ValidationError(message string) *ApiError {
return &ApiError{Code: -1, Desc: message}
}
func MarketNotFoundError(marketID string) *ApiError {
return &ApiError{Code: -3, Desc: fmt.Sprintf("not support marketID: %s", marketID)}
}
func InvalidPriceAmountError() *ApiError {
return &ApiError{Code: -4, Desc: fmt.Sprintf("price and amount should be positive number")}
}
func buildErrorMessage(errors validator.ValidationErrors) string {
buff := bytes.Buffer{}
for _, err := range errors {
buff.WriteString(buildSingleError(err))
buff.WriteString(";")
}
return buff.String()
}
func buildSingleError(err validator.FieldError) string {
switch err.Tag() {
case "required":
return fmt.Sprintf("%s is required", err.Field())
default:
return fmt.Sprintf("Key: '%s' Error:Field validation for '%s' failed on the '%s' tag", err.Namespace(), err.Field(), err.Tag())
}
}