Skip to content

Commit

Permalink
chore: refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Kape committed Sep 26, 2023
1 parent df52258 commit 607911c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
5 changes: 2 additions & 3 deletions cmd/debug/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/samirkape/awesome-go-bot/domain/gopackage/mongodb"
"github.com/samirkape/awesome-go-bot/gobot"
"github.com/samirkape/awesome-go-bot/gobot/config"
"github.com/samirkape/awesome-go-bot/internal/errors"
"github.com/samirkape/awesome-go-bot/internal/logger"
"github.com/samirkape/awesome-go-bot/internal/services/chat/factory"
"github.com/samirkape/awesome-go-bot/internal/services/internalerrors"
"github.com/samirkape/awesome-go-bot/internal/services/packages"
"github.com/samirkape/awesome-go-bot/internal/services/packages/analytics"
"github.com/samirkape/awesome-go-bot/internal/services/packages/search"
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestMemory(t *testing.T) {
// create new chat
newChat, err := factory.New(&update)
if err != nil {
internalerrors.RespondToError(err, botService, newChat)
errors.RespondToError(err, botService, newChat)
}
chatService, err := factory.NewService(newChat, analyticsService, searchService, botService)
log.Println(err)
Expand All @@ -66,7 +66,6 @@ func TestMemory(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
return
}
}

Expand Down
11 changes: 6 additions & 5 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/samirkape/awesome-go-bot/domain/gopackage/mongodb"
"github.com/samirkape/awesome-go-bot/gobot"
"github.com/samirkape/awesome-go-bot/gobot/config"
"github.com/samirkape/awesome-go-bot/internal/errors"
"github.com/samirkape/awesome-go-bot/internal/logger"
chatfactory "github.com/samirkape/awesome-go-bot/internal/services/chat/factory"
"github.com/samirkape/awesome-go-bot/internal/services/internalerrors"
"github.com/samirkape/awesome-go-bot/internal/services/packages"
"github.com/samirkape/awesome-go-bot/internal/services/packages/analytics"
"github.com/samirkape/awesome-go-bot/internal/services/packages/search"
Expand All @@ -17,7 +17,7 @@ import (
"net/http"
)

func HandleTelegramWebHook(w http.ResponseWriter, r *http.Request) {
func HandleTelegramWebHook(_ http.ResponseWriter, r *http.Request) {
request, err := parseRequest(r.Body)
if err != nil {
log.Fatal("parsing error")
Expand All @@ -37,21 +37,22 @@ func ExecuteCommand(incomingRequest *tgbotapi.Update) error {
// create new chat
chatInfo, err := chatfactory.New(incomingRequest)
if err != nil {
internalerrors.RespondToError(err, botService, chatInfo)
errors.RespondToError(err, botService, chatInfo)
return err
}
// create new mongodb client
client, err := mongodb.New(mongodb.WithDefaultConfig())
if err != nil {
return err
}
// create package service from mongodb client
packageService := packages.NewService(client)
// get all analyticsService from the database
// create analytics interface from package service
analyticsService := analytics.NewService(packageService)
if err != nil {
return err
}
// create new search service
// create new search service based on package service
searchService := search.NewService(packageService)
// create new chat service
chatService, err := chatfactory.NewService(chatInfo, analyticsService, searchService, botService)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internalerrors
package errors

import (
"errors"
Expand Down
6 changes: 3 additions & 3 deletions internal/services/chat/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package inline

import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samirkape/awesome-go-bot/internal/errors"
"github.com/samirkape/awesome-go-bot/internal/services/chat"
"github.com/samirkape/awesome-go-bot/internal/services/internalerrors"
"github.com/samirkape/awesome-go-bot/internal/services/packages/analytics/inmemory"
"github.com/samirkape/awesome-go-bot/internal/services/packages/search"
"log"
Expand All @@ -19,7 +19,7 @@ type inlineChat struct {

func NewChat(update *tgbotapi.Update) (chat.Info, error) {
if update.InlineQuery == nil {
return &chat.Chat{}, internalerrors.NewValidationError("inline query is nil")
return &chat.Chat{}, errors.NewValidationError("inline query is nil")
}
query := strings.TrimSpace(update.InlineQuery.Query)
return inlineChat{Info: &chat.Chat{
Expand Down Expand Up @@ -61,7 +61,7 @@ func (i inlineChat) HandleQuery() error {
}

func createInlineQueryArticle(packages []inmemory.Package, results []interface{}) []interface{} {
for i, _ := range packages {
for i := range packages {
article := tgbotapi.NewInlineQueryResultArticle(
strconv.Itoa(i),
packages[i].Name,
Expand Down
4 changes: 2 additions & 2 deletions internal/services/chat/keyboard/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/samirkape/awesome-go-bot/domain/gopackage/helpers"
"github.com/samirkape/awesome-go-bot/gobot"
"github.com/samirkape/awesome-go-bot/gobot/commands"
"github.com/samirkape/awesome-go-bot/internal/errors"
"github.com/samirkape/awesome-go-bot/internal/services/chat"
"github.com/samirkape/awesome-go-bot/internal/services/internalerrors"
"github.com/samirkape/awesome-go-bot/internal/services/packages/analytics"
"github.com/samirkape/awesome-go-bot/internal/services/packages/analytics/inmemory"
)
Expand All @@ -24,7 +24,7 @@ type keyboardChat struct {

func NewChat(update *tgbotapi.Update) (chat.Info, error) {
if update.CallbackQuery == nil {
return nil, internalerrors.NewValidationError("callback query is nil")
return nil, errors.NewValidationError("callback query is nil")
}
query := update.CallbackQuery
return &keyboardChat{
Expand Down
10 changes: 5 additions & 5 deletions internal/services/chat/regular/regular.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/samirkape/awesome-go-bot/gobot"
"github.com/samirkape/awesome-go-bot/gobot/commands"
"github.com/samirkape/awesome-go-bot/gobot/constant"
"github.com/samirkape/awesome-go-bot/internal/errors"
"github.com/samirkape/awesome-go-bot/internal/services/chat"
"github.com/samirkape/awesome-go-bot/internal/services/chat/keyboard"
"github.com/samirkape/awesome-go-bot/internal/services/internalerrors"
"github.com/samirkape/awesome-go-bot/internal/services/packages/analytics"
"strconv"
"strings"
Expand All @@ -28,12 +28,12 @@ var nonNumericQueryError = "query cannot be parsed, try"

func NewValidatedChat(update *tgbotapi.Update) (chat.Info, error) {
if update.Message == nil {
return nil, internalerrors.NewValidationError(emptyUpdateError)
return nil, errors.NewValidationError(emptyUpdateError)
}

query := strings.TrimSpace(update.Message.Text)
if query == "" {
return nil, internalerrors.NewValidationError(emptyQueryError)
return nil, errors.NewValidationError(emptyQueryError)
}

chatId := update.Message.Chat.ID
Expand All @@ -42,9 +42,9 @@ func NewValidatedChat(update *tgbotapi.Update) (chat.Info, error) {
_, err := strconv.Atoi(query)
if err == nil {
updatedQuery := fmt.Sprintf("%s%s", constant.CommandPrefix, query)
return newRegular(chatId, query), internalerrors.NewValidationError(invalidQueryError, updatedQuery)
return newRegular(chatId, query), errors.NewValidationError(invalidQueryError, updatedQuery)
} else if update.Message.ViaBot == nil {
return newRegular(chatId, query), internalerrors.NewValidationError(nonNumericQueryError, startCommand)
return newRegular(chatId, query), errors.NewValidationError(nonNumericQueryError, startCommand)
}
}

Expand Down

0 comments on commit 607911c

Please sign in to comment.