Browse Source
feat: introduce error package (#40)
feat: introduce error package (#40)
* feat: introduce error and add test validation * feat: update UIpull/41/head v4.2.0
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 800 additions and 244 deletions
-
7src/cmd/root.go
-
2src/config/settings.go
-
26src/internal/rest/app.go
-
22src/internal/rest/middleware/recovery.go
-
50src/internal/rest/send.go
-
21src/internal/rest/user.go
-
19src/pkg/error/auth_error.go
-
27src/pkg/error/generic_error.go
-
19src/pkg/error/validation_error.go
-
24src/pkg/error/whatsapp_error.go
-
12src/pkg/utils/general.go
-
3src/pkg/utils/response.go
-
30src/pkg/whatsapp/whatsapp.go
-
0src/services/app.go
-
100src/services/send.go
-
28src/services/user.go
-
29src/utils/errors.go
-
82src/validations/send_validation.go
-
431src/validations/send_validation_test.go
-
18src/validations/user_validation.go
-
74src/validations/user_validation_test.go
-
20src/views/index.html
@ -0,0 +1,19 @@ |
|||
package error |
|||
|
|||
import "net/http" |
|||
|
|||
type AuthError string |
|||
|
|||
func (err AuthError) Error() string { |
|||
return string(err) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (err AuthError) ErrCode() string { |
|||
return "AUTHENTICATION_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (err AuthError) StatusCode() int { |
|||
return http.StatusUnauthorized |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package error |
|||
|
|||
import "net/http" |
|||
|
|||
// GenericError represent as the contract of generic error
|
|||
type GenericError interface { |
|||
Error() string |
|||
ErrCode() string |
|||
StatusCode() int |
|||
} |
|||
|
|||
type InternalServerError string |
|||
|
|||
// Error for complying the error interface
|
|||
func (e InternalServerError) Error() string { |
|||
return string(e) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (e InternalServerError) ErrCode() string { |
|||
return "INTERNAL_SERVER_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (e InternalServerError) StatusCode() int { |
|||
return http.StatusInternalServerError |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
package error |
|||
|
|||
import "net/http" |
|||
|
|||
type ValidationError string |
|||
|
|||
func (err ValidationError) Error() string { |
|||
return string(err) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (err ValidationError) ErrCode() string { |
|||
return "VALIDATION_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (err ValidationError) StatusCode() int { |
|||
return http.StatusBadRequest |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
package error |
|||
|
|||
import "net/http" |
|||
|
|||
type InvalidJID string |
|||
|
|||
// Error for complying the error interface
|
|||
func (e InvalidJID) Error() string { |
|||
return string(e) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (e InvalidJID) ErrCode() string { |
|||
return "INVALID_JID" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (e InvalidJID) StatusCode() int { |
|||
return http.StatusBadRequest |
|||
} |
|||
|
|||
const ( |
|||
ErrInvalidJID = InvalidJID("your JID is invalid") |
|||
) |
|||
@ -1,7 +1,8 @@ |
|||
package utils |
|||
|
|||
type ResponseData struct { |
|||
Code int `json:"code"` |
|||
Status int |
|||
Code string `json:"code,omitempty"` |
|||
Message string `json:"message"` |
|||
Results any `json:"results"` |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
package utils |
|||
|
|||
import "fmt" |
|||
|
|||
func PanicIfNeeded(err any, message ...string) { |
|||
if err != nil { |
|||
if fmt.Sprintf("%s", err) == "record not found" && len(message) > 0 { |
|||
panic(message[0]) |
|||
} else { |
|||
panic(err) |
|||
} |
|||
} |
|||
} |
|||
|
|||
type ValidationError struct { |
|||
Message string |
|||
} |
|||
|
|||
func (validationError ValidationError) Error() string { |
|||
return validationError.Message |
|||
} |
|||
|
|||
type AuthError struct { |
|||
Message string |
|||
} |
|||
|
|||
func (err AuthError) Error() string { |
|||
return err.Message |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
package validations |
|||
|
|||
import ( |
|||
domainUser "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/user" |
|||
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error" |
|||
"github.com/stretchr/testify/assert" |
|||
"testing" |
|||
) |
|||
|
|||
func TestValidateUserAvatar(t *testing.T) { |
|||
type args struct { |
|||
request domainUser.AvatarRequest |
|||
} |
|||
tests := []struct { |
|||
name string |
|||
args args |
|||
err any |
|||
}{ |
|||
{ |
|||
name: "should success", |
|||
args: args{request: domainUser.AvatarRequest{ |
|||
Phone: "1728937129312@s.whatsapp.net", |
|||
}}, |
|||
err: nil, |
|||
}, |
|||
{ |
|||
name: "should error with empty phone", |
|||
args: args{request: domainUser.AvatarRequest{ |
|||
Phone: "", |
|||
}}, |
|||
err: pkgError.ValidationError("phone: cannot be blank."), |
|||
}, |
|||
} |
|||
|
|||
for _, tt := range tests { |
|||
t.Run(tt.name, func(t *testing.T) { |
|||
err := ValidateUserAvatar(tt.args.request) |
|||
assert.Equal(t, tt.err, err) |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func TestValidateUserInfo(t *testing.T) { |
|||
type args struct { |
|||
request domainUser.InfoRequest |
|||
} |
|||
tests := []struct { |
|||
name string |
|||
args args |
|||
err any |
|||
}{ |
|||
{ |
|||
name: "should success", |
|||
args: args{request: domainUser.InfoRequest{ |
|||
Phone: "1728937129312@s.whatsapp.net", |
|||
}}, |
|||
err: nil, |
|||
}, |
|||
{ |
|||
name: "should error with empty phone", |
|||
args: args{request: domainUser.InfoRequest{ |
|||
Phone: "", |
|||
}}, |
|||
err: pkgError.ValidationError("phone: cannot be blank."), |
|||
}, |
|||
} |
|||
|
|||
for _, tt := range tests { |
|||
t.Run(tt.name, func(t *testing.T) { |
|||
err := ValidateUserInfo(tt.args.request) |
|||
assert.Equal(t, tt.err, err) |
|||
}) |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue