-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
106 lines (89 loc) · 2.79 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
95
96
97
98
99
100
101
102
103
104
105
106
import (
"github.com/gofiber/fiber/v2"
"github.com/pkg/errors"
"net/http"
)
// ErrorType is the type of an error
type ErrorType uint
const (
// NoType error
ErrorTypeNoType ErrorType = iota
ErrorTypeBadRequest
ErrorTypeNotFound
ErrorTypeInternal
ErrorTypeNotLogged
)
type myError struct {
errorType ErrorType
originalError error
customError string
}
func NewError(errorType ErrorType, msg string) error {
return myError{errorType: errorType, originalError: errors.New(msg)}
}
func ErrorBadRequest(msg string) error {
return myError{errorType: ErrorTypeBadRequest, originalError: errors.New(msg)}
}
func ErrorNotLogged(msg string) error {
return myError{errorType: ErrorTypeNotLogged, originalError: errors.New(msg)}
}
func ErrorVeritabani(err error, msg string, args ...interface{}) error {
wrappedError := errors.Wrapf(err, msg, args...)
return myError{errorType: ErrorTypeInternal, originalError: wrappedError, customError: "veritabanı hatası"}
}
func ErrorInternalError(err error, msg string, args ...interface{}) error {
wrappedError := errors.Wrapf(err, msg, args...)
return myError{errorType: ErrorTypeInternal, originalError: wrappedError, customError: "sunucu içi hata"}
}
// Error returns the mssage of a myError
func (error myError) Error() string {
if error.originalError == nil {
return error.customError
}
return error.originalError.Error()
}
// New creates a no type error
func New(msg string) error {
return myError{errorType: ErrorTypeNoType, originalError: errors.New(msg)}
}
// GetType returns the error type
func GetType(err error) ErrorType {
if customErr, ok := err.(myError); ok {
return customErr.errorType
}
return ErrorTypeNoType
}
// ErrorHandler fiber için
func ErrorHandler(c *fiber.Ctx, err error) error {
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
respModel := &context.ResponseModel{
HataVarMi: true,
Message: "internal server error",
}
ctxRqId := c.Get("requestid", "")
logger := config.Logger(ctxRqId)
if GetType(err) == ErrorTypeNoType {
if err != nil {
logger.Error(err.Error())
}
return c.Status(http.StatusInternalServerError).JSON(respModel)
}
myerr := err.(myError)
switch myerr.errorType {
case ErrorTypeNotFound:
respModel.Message = "bulunamadı"
return c.Status(http.StatusNotFound).JSON(respModel)
case ErrorTypeBadRequest:
respModel.Message = myerr.Error()
return c.Status(http.StatusBadRequest).JSON(respModel)
case ErrorTypeInternal:
respModel.Message = myerr.customError
logger.Error(myerr.Error())
return c.Status(http.StatusInternalServerError).JSON(respModel)
case ErrorTypeNotLogged:
respModel.Message = myerr.Error()
return c.Status(http.StatusUnauthorized).JSON(respModel)
}
logger.Error(myerr.Error())
return c.Status(http.StatusInternalServerError).JSON(respModel)
}