Skip to content

Commit

Permalink
Use errors.Is for comparing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xoltia committed Oct 13, 2023
1 parent 965510c commit f7f3878
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/bot/commands/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (c *ChartCommand) Handle(ctx *bot.InteractionContext) error {
guildID := ctx.Interaction().GuildID
user, err := c.ur.FindByID(ctx.ResponseContext(), userID)

if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
return ctx.Respond(discordgo.InteractionResponseChannelMessageWithSource, &discordgo.InteractionResponseData{
Content: "You have no activity!",
})
Expand All @@ -301,7 +301,7 @@ func (c *ChartCommand) Handle(ctx *bot.InteractionContext) error {
} else if guildID != "" {
guild, err := c.gr.FindByID(ctx.ResponseContext(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/bot/commands/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *LeaderboardCommand) Handle(ctx *bot.InteractionContext) error {
user, err := c.u.FindByID(ctx.Context(), i.Member.User.ID)
guildID := i.GuildID

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand All @@ -127,7 +127,7 @@ func (c *LeaderboardCommand) Handle(ctx *bot.InteractionContext) error {
} else if guildID != "" {
guild, err := c.g.FindByID(ctx.ResponseContext(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down
18 changes: 9 additions & 9 deletions internal/bot/commands/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ func (c *LogCommand) Handle(ctx *bot.InteractionContext) error {
func (c *LogCommand) handleAutocomplete(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) error {
data := i.ApplicationCommandData()
subcommand := data.Options[0].Name
focuedOption := discordutil.GetFocusedOption(data.Options[0].Options)
focusedOption := discordutil.GetFocusedOption(data.Options[0].Options)

if focuedOption == nil {
if focusedOption == nil {
return nil
}

if focuedOption.Name != "name" {
if focusedOption.Name != "name" {
return nil
}

Expand All @@ -342,7 +342,7 @@ func (c *LogCommand) handleAutocomplete(ctx context.Context, s *discordgo.Sessio
mediaType = activities.ActivityMediaTypeVisualNovel
}

input := focuedOption.StringValue()
input := focusedOption.StringValue()
results, err := c.createAutocompleteResult(ctx, mediaType, input)

if err != nil {
Expand Down Expand Up @@ -431,7 +431,7 @@ func (c *LogCommand) handleAnime(ctx *bot.InteractionContext, subcommand *discor
} else if guildID != "" {
guild, err := c.guildRepo.FindByID(ctx.Context(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down Expand Up @@ -567,7 +567,7 @@ func (c *LogCommand) handleBook(ctx *bot.InteractionContext, subcommand *discord
} else if guildID != "" {
guild, err := c.guildRepo.FindByID(ctx.Context(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down Expand Up @@ -712,7 +712,7 @@ func (c *LogCommand) handleVisualNovel(ctx *bot.InteractionContext, subcommand *
} else if guildID != "" {
guild, err := c.guildRepo.FindByID(ctx.Context(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down Expand Up @@ -813,7 +813,7 @@ func (c *LogCommand) handleVideo(ctx *bot.InteractionContext, subcommand *discor
} else if guildID != "" {
guild, err := c.guildRepo.FindByID(ctx.Context(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down Expand Up @@ -928,7 +928,7 @@ func (c *LogCommand) handleManual(ctx *bot.InteractionContext, subcommand *disco
} else if guildID != "" {
guild, err := c.guildRepo.FindByID(ctx.Context(), guildID)

if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/bot/commands/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *UndoCommand) Handle(ctx *bot.InteractionContext) error {
func (c *UndoCommand) undoActivity(ctx *bot.InteractionContext, id uint64) error {
activity, err := c.r.GetByID(ctx.ResponseContext(), id, ctx.Interaction().GuildID)

if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
return ctx.Respond(discordgo.InteractionResponseChannelMessageWithSource, &discordgo.InteractionResponseData{
Content: "Activity not found.",
})
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *UndoCommand) undoLastActivity(ctx *bot.InteractionContext) error {
userID := discordutil.GetInteractionUser(ctx.Interaction()).ID
activity, err := c.r.GetLatestByUserID(ctx.ResponseContext(), userID, ctx.Interaction().GuildID)

if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
return ctx.Respond(discordgo.InteractionResponseChannelMessageWithSource, &discordgo.InteractionResponseData{
Content: "You have no activities to undo.",
})
Expand Down
3 changes: 2 additions & 1 deletion internal/guilds/guild.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package guilds

import (
"context"
"errors"
"sync"

"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -97,7 +98,7 @@ func (r *GuildRepository) FindOrCreate(ctx context.Context, id string) (*Guild,
guild, err := r.FindByID(ctx, id)

if err != nil {
if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
guild = NewGuild(id)
err = r.Create(ctx, guild)

Expand Down

0 comments on commit f7f3878

Please sign in to comment.