11package utils
22
33import (
4+ "context"
45 "fmt"
56 "net/http"
67 "strings"
78
89 "github.com/hammer-code/lms-be/domain"
10+ "github.com/hammer-code/lms-be/pkg/ngelog"
911)
1012
1113const (
@@ -27,23 +29,26 @@ func (e *CustomHttpError) Error() string {
2729 return e .Message
2830}
2931
30- func NewBadRequestError (msg string , err error ) * CustomHttpError {
32+ func NewBadRequestError (ctx context.Context , msg string , err error ) * CustomHttpError {
33+ ngelog .Error (ctx , msg , err )
3134 return & CustomHttpError {
3235 Code : http .StatusBadRequest ,
3336 Message : msg ,
3437 OriginError : err ,
3538 }
3639}
3740
38- func NewUnauthorizedError (msg string , err error ) * CustomHttpError {
41+ func NewUnauthorizedError (ctx context.Context , msg string , err error ) * CustomHttpError {
42+ ngelog .Error (ctx , msg , err )
3943 return & CustomHttpError {
4044 Code : http .StatusUnauthorized ,
4145 Message : msg ,
4246 OriginError : err ,
4347 }
4448}
4549
46- func NewInternalServerError (err error ) * CustomHttpError {
50+ func NewInternalServerError (ctx context.Context , err error ) * CustomHttpError {
51+ ngelog .Error (ctx , "Internal server error" , err )
4752 return & CustomHttpError {
4853 Code : http .StatusInternalServerError ,
4954 Message : "Internal server error" ,
@@ -62,18 +67,17 @@ func CheckError(errStr, containsStr, message string, code int) (domain.HttpRespo
6267}
6368
6469func CustomErrorResponse (err error ) domain.HttpResponse {
65- fmt .Println ("YOW" , err )
6670 if customErr , ok := err .(* CustomHttpError ); ok {
6771 var errStr string
6872 if customErr .OriginError != nil {
6973 errStr = customErr .OriginError .Error ()
7074 }
71- resp , ok := CheckError (errStr , ErrDuplicateEmail , "User already exist" , 400 )
75+ resp , ok := CheckError (errStr , ErrDuplicateEmail , "User already exist" , http . StatusBadRequest )
7276 if ok {
7377 return resp
7478 }
7579
76- resp , ok = CheckError (errStr , ErrWrongPassword , "Sorry, your password is incorrect" , 400 )
80+ resp , ok = CheckError (errStr , ErrWrongPassword , "Sorry, your password is incorrect" , http . StatusBadRequest )
7781 if ok {
7882 return resp
7983 }
0 commit comments