/
errJSONUnmarshal.go
54 lines (44 loc) · 1.09 KB
/
errJSONUnmarshal.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
package errorhandler
import (
"fmt"
"github.com/cockroachdb/errors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net/http"
)
type ErrJSONUnmarshal struct {
system string
err error
}
func (e *ErrJSONUnmarshal) SetSystem(system string) IErrorReport {
if e.system == "" {
e.system = system
}
return e
}
// GetName method
func (e ErrJSONUnmarshal) GetName() string {
return ErrJsonUnmarshal
}
func (e ErrJSONUnmarshal) GetError() error {
return e.err
}
func (e ErrJSONUnmarshal) Error() string {
return fmt.Sprintln("[ERROR]:", e.err.Error())
}
func (e ErrJSONUnmarshal) Report(prefix string) {
logger.Warn(prefix, zap.Error(e.GetError()))
}
func (e ErrJSONUnmarshal) GinReport(c *gin.Context) {
c.AbortWithError(http.StatusBadRequest, e.err)
}
func (e ErrJSONUnmarshal) GRPCReport(errContent *error, prefixMessage string) {
*errContent = status.Error(codes.InvalidArgument, errors.Wrap(e.err, prefixMessage).Error())
}
func NewErrJSONUnmarshal(err error) *ErrJSONUnmarshal {
return &ErrJSONUnmarshal{
err: err,
}
}