/
errNotFound.go
54 lines (44 loc) · 1.02 KB
/
errNotFound.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 ErrNotFound struct {
system string
err error
}
func (e *ErrNotFound) SetSystem(system string) IErrorReport {
if e.system == "" {
e.system = system
}
return e
}
// GetName method
func (e ErrNotFound) GetName() string {
return ErrDataNotFound
}
func (e ErrNotFound) GetError() error {
return e.err
}
func (e ErrNotFound) Error() string {
return fmt.Sprintln("[ERROR]:", e.err.Error())
}
func (e ErrNotFound) Report(prefix string) {
logger.Warn(prefix, zap.Error(e.GetError()))
}
func (e ErrNotFound) GinReport(c *gin.Context) {
c.AbortWithError(http.StatusNotFound, e.err)
}
func (e ErrNotFound) GRPCReport(errContent *error, prefixMessage string) {
*errContent = status.Error(codes.NotFound, errors.Wrap(e.err, prefixMessage).Error())
}
func NewErrNotFound(err error) *ErrNotFound {
return &ErrNotFound{
err: err,
}
}