diff --git a/middleware/recovery.go b/middleware/recovery.go index 64ebf4f..eb9a632 100644 --- a/middleware/recovery.go +++ b/middleware/recovery.go @@ -12,14 +12,19 @@ func Recovery() fiber.Handler { err := recover() if err != nil { var res utils.ResponseData + res.Code = 500 + res.Message = fmt.Sprintf("%s", err) - dt, ok := err.(utils.ValidationError) - if ok { + errValidation, okValidation := err.(utils.ValidationError) + if okValidation { res.Code = 400 - res.Message = dt.Message - } else { - res.Code = 500 - res.Message = fmt.Sprintf("%s", err) + res.Message = errValidation.Message + } + + errAuth, okAuth := err.(utils.AuthError) + if okAuth { + res.Code = 401 + res.Message = errAuth.Message } _ = ctx.Status(res.Code).JSON(res) diff --git a/services/send_service_impl.go b/services/send_service_impl.go index 94b3260..41f6349 100644 --- a/services/send_service_impl.go +++ b/services/send_service_impl.go @@ -27,10 +27,9 @@ func NewSendService(waCli *whatsmeow.Client) SendService { } } -func (service SendServiceImpl) SendText(c *fiber.Ctx, request structs.SendMessageRequest) (response structs.SendMessageResponse, err error) { +func (service SendServiceImpl) SendText(_ *fiber.Ctx, request structs.SendMessageRequest) (response structs.SendMessageResponse, err error) { if !service.WaCli.IsLoggedIn() { - err = errors.New("you are not loggin") - return + panic(utils.AuthError{Message: "you are not loggin"}) } recipient, ok := utils.ParseJID(request.PhoneNumber) if !ok { @@ -48,10 +47,8 @@ func (service SendServiceImpl) SendText(c *fiber.Ctx, request structs.SendMessag func (service SendServiceImpl) SendImage(c *fiber.Ctx, request structs.SendImageRequest) (response structs.SendImageResponse, err error) { if !service.WaCli.IsLoggedIn() { - err = errors.New("you are not loggin") - return + panic(utils.AuthError{Message: "you are not loggin"}) } - // Resize image oriImagePath := fmt.Sprintf("%s/%s", config.PathSendItems, request.Image.Filename) err = c.SaveFile(request.Image, oriImagePath) @@ -120,10 +117,8 @@ func (service SendServiceImpl) SendImage(c *fiber.Ctx, request structs.SendImage func (service SendServiceImpl) SendFile(c *fiber.Ctx, request structs.SendFileRequest) (response structs.SendFileResponse, err error) { if !service.WaCli.IsLoggedIn() { - err = errors.New("you are not loggin") - return + panic(utils.AuthError{Message: "you are not loggin"}) } - // Resize image oriFilePath := fmt.Sprintf("%s/%s", config.PathSendItems, request.File.Filename) err = c.SaveFile(request.File, oriFilePath) @@ -158,25 +153,15 @@ func (service SendServiceImpl) SendFile(c *fiber.Ctx, request structs.SendFileRe } msg := &waProto.Message{DocumentMessage: &waProto.DocumentMessage{ - Url: proto.String(uploadedFile.URL), - Mimetype: proto.String(http.DetectContentType(dataWaFile)), - Title: proto.String(request.File.Filename), - FileSha256: uploadedFile.FileSHA256, - FileLength: proto.Uint64(uploadedFile.FileLength), - PageCount: nil, - MediaKey: uploadedFile.MediaKey, - FileName: proto.String(request.File.Filename), - FileEncSha256: uploadedFile.FileEncSHA256, - DirectPath: proto.String(uploadedFile.DirectPath), - MediaKeyTimestamp: nil, - ContactVcard: nil, - ThumbnailDirectPath: nil, - ThumbnailSha256: nil, - ThumbnailEncSha256: nil, - JpegThumbnail: nil, - ContextInfo: nil, - ThumbnailHeight: nil, - ThumbnailWidth: nil, + Url: proto.String(uploadedFile.URL), + Mimetype: proto.String(http.DetectContentType(dataWaFile)), + Title: proto.String(request.File.Filename), + FileSha256: uploadedFile.FileSHA256, + FileLength: proto.Uint64(uploadedFile.FileLength), + MediaKey: uploadedFile.MediaKey, + FileName: proto.String(request.File.Filename), + FileEncSha256: uploadedFile.FileEncSHA256, + DirectPath: proto.String(uploadedFile.DirectPath), }} ts, err := service.WaCli.SendMessage(dataWaRecipient, "", msg) go removeFile(oriFilePath) diff --git a/services/user_service_impl.go b/services/user_service_impl.go index 3c52866..5ef850b 100644 --- a/services/user_service_impl.go +++ b/services/user_service_impl.go @@ -20,10 +20,9 @@ func NewUserService(waCli *whatsmeow.Client) UserService { } } -func (service UserServiceImpl) UserInfo(c *fiber.Ctx, request structs.UserInfoRequest) (response structs.UserInfoResponse, err error) { +func (service UserServiceImpl) UserInfo(_ *fiber.Ctx, request structs.UserInfoRequest) (response structs.UserInfoResponse, err error) { if !service.WaCli.IsLoggedIn() { - err = errors.New("you are not loggin") - return + panic(utils.AuthError{Message: "you are not loggin"}) } var jids []types.JID jid, ok := utils.ParseJID(request.PhoneNumber) @@ -63,10 +62,9 @@ func (service UserServiceImpl) UserInfo(c *fiber.Ctx, request structs.UserInfoRe return response, nil } -func (service UserServiceImpl) UserAvatar(c *fiber.Ctx, request structs.UserAvatarRequest) (response structs.UserAvatarResponse, err error) { +func (service UserServiceImpl) UserAvatar(_ *fiber.Ctx, request structs.UserAvatarRequest) (response structs.UserAvatarResponse, err error) { if !service.WaCli.IsLoggedIn() { - err = errors.New("you are not loggin") - return + panic(utils.AuthError{Message: "you are not loggin"}) } jid, ok := utils.ParseJID(request.PhoneNumber) if !ok { diff --git a/utils/errors.go b/utils/errors.go index 52ff9f1..cec7f77 100644 --- a/utils/errors.go +++ b/utils/errors.go @@ -19,3 +19,11 @@ type ValidationError struct { func (validationError ValidationError) Error() string { return validationError.Message } + +type AuthError struct { + Message string +} + +func (err AuthError) Error() string { + return err.Message +}