11package utils
22
33import (
4+ "context"
5+ "fmt"
6+ "net/http"
47 "strings"
58
69 "github.com/hammer-code/lms-be/domain"
10+ "github.com/hammer-code/lms-be/pkg/ngelog"
711)
812
9- func CheckError (err , sub , message string , code int ) (domain.HttpResponse , bool ) {
13+ const (
14+ ErrDuplicateEmail = "\" uni_users_email\" (SQLSTATE 23505)"
15+ ErrWrongPassword = "password"
16+ ErrNotFoundSQL = "sql: no rows in result set"
17+ )
18+
19+ type CustomHttpError struct {
20+ Code int
21+ Message string
22+ OriginError error
23+ }
24+
25+ func (e * CustomHttpError ) Error () string {
26+ if e .OriginError != nil {
27+ return fmt .Sprintf ("%s: %s" , e .Message , e .OriginError .Error ())
28+ }
29+ return e .Message
30+ }
1031
11- if strings .Contains (err , sub ) {
32+ func NewBadRequestError (ctx context.Context , msg string , err error ) * CustomHttpError {
33+ ngelog .Error (ctx , msg , err )
34+ return & CustomHttpError {
35+ Code : http .StatusBadRequest ,
36+ Message : msg ,
37+ OriginError : err ,
38+ }
39+ }
40+
41+ func NewUnauthorizedError (ctx context.Context , msg string , err error ) * CustomHttpError {
42+ ngelog .Error (ctx , msg , err )
43+ return & CustomHttpError {
44+ Code : http .StatusUnauthorized ,
45+ Message : msg ,
46+ OriginError : err ,
47+ }
48+ }
49+
50+ func NewInternalServerError (ctx context.Context , err error ) * CustomHttpError {
51+ ngelog .Error (ctx , "Internal server error" , err )
52+ return & CustomHttpError {
53+ Code : http .StatusInternalServerError ,
54+ Message : "Internal server error" ,
55+ OriginError : err ,
56+ }
57+ }
58+
59+ func CheckError (errStr , containsStr , message string , code int ) (domain.HttpResponse , bool ) {
60+ if strings .Contains (errStr , containsStr ) {
1261 return domain.HttpResponse {
1362 Code : code ,
1463 Message : message ,
@@ -17,25 +66,35 @@ func CheckError(err, sub, message string, code int) (domain.HttpResponse, bool)
1766 return domain.HttpResponse {}, false
1867}
1968
20- func CostumErr (err string ) domain.HttpResponse {
69+ func CustomErrorResponse (err error ) domain.HttpResponse {
70+ if customErr , ok := err .(* CustomHttpError ); ok {
71+ var errStr string
72+ if customErr .OriginError != nil {
73+ errStr = customErr .OriginError .Error ()
74+ }
75+ resp , ok := CheckError (errStr , ErrDuplicateEmail , "User already exist" , http .StatusBadRequest )
76+ if ok {
77+ return resp
78+ }
2179
22- resp , ok : = CheckError (err , " \" uni_users_email \" (SQLSTATE 23505)" , "User already exist " , 400 )
23- if ok {
24- return resp
25- }
80+ resp , ok = CheckError (errStr , ErrWrongPassword , "Sorry, your password is incorrect " , http . StatusBadRequest )
81+ if ok {
82+ return resp
83+ }
2684
27- resp , ok = CheckError (err , " \" uni_logout_token \" (SQLSTATE 23505)" , "You have already logged out. " , 400 )
28- if ok {
29- return resp
30- }
85+ resp , ok = CheckError (errStr , ErrNotFoundSQL , "Data not found " , http . StatusNotFound )
86+ if ok {
87+ return resp
88+ }
3189
32- resp , ok = CheckError (err , "password" , "Sorry, your password is incorrect" , 400 )
33- if ok {
34- return resp
90+ return domain.HttpResponse {
91+ Code : customErr .Code ,
92+ Message : customErr .Message ,
93+ }
3594 }
3695
3796 return domain.HttpResponse {
38- Code : 400 ,
97+ Code : http . StatusInternalServerError ,
3998 Message : "Internal server error" ,
4099 }
41100
0 commit comments