use errors.New where it is possible instead of fmt.Errorf

This commit is contained in:
Dawid Wysokiński 2021-05-05 19:49:00 +02:00
parent 0a271d1255
commit 49b407ed23
16 changed files with 39 additions and 42 deletions

View File

@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -45,9 +45,9 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg dailytribestats.FetchCo
}
if err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
return nil, 0, fmt.Errorf("Server not found")
return nil, 0, errors.New("Server not found")
}
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -5,7 +5,7 @@ package generated
import (
"bytes"
"context"
"errors"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strconv"
"sync"

View File

@ -2,7 +2,6 @@ package usecase
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -43,7 +42,7 @@ func (ucase *usecase) GetByID(ctx context.Context, server string, id int) (*twmo
return nil, err
}
if len(players) == 0 {
return nil, fmt.Errorf("Player (ID: %d) not found.", id)
return nil, errors.Errorf("Player (ID: %d) not found.", id)
}
return players[0], nil
}
@ -53,7 +52,7 @@ func (ucase *usecase) SearchPlayer(ctx context.Context, cfg player.SearchPlayerC
return nil, 0, errors.New("Version is required.")
}
if "" == strings.TrimSpace(cfg.Name) && cfg.ID <= 0 {
return nil, 0, errors.New("Your search is ambiguous. You must specify the variable 'name' or 'id'.")
return nil, 0, errors.New("Query is too ambiguous. You must specify the variable 'name' or 'id'.")
}
if !middleware.CanExceedLimit(ctx) && (cfg.Limit > player.FetchLimit || cfg.Limit <= 0) {
cfg.Limit = player.FetchLimit

View File

@ -2,7 +2,6 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/tribalwarshelp/shared/tw/twmodel"
@ -52,7 +51,7 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg server.FetchConfig) ([]
total, err = query.Count()
}
if err != nil && err != pg.ErrNoRows {
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -2,7 +2,7 @@ package usecase
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"github.com/tribalwarshelp/api/middleware"
@ -40,7 +40,7 @@ func (ucase *usecase) GetByKey(ctx context.Context, key string) (*twmodel.Server
return nil, err
}
if len(servers) == 0 {
return nil, fmt.Errorf("Server (key: %s) not found.", key)
return nil, errors.Errorf("Server (key: %s) not found.", key)
}
return servers[0], nil
}

View File

@ -3,6 +3,7 @@ package httpdelivery
import (
"fmt"
"github.com/Kichiyaki/appmode"
"github.com/pkg/errors"
"net/http"
"strconv"
"time"
@ -34,7 +35,7 @@ type handler struct {
func Attach(cfg Config) error {
if cfg.MapUsecase == nil {
return fmt.Errorf("cfg.MapUsecase cannot be nil")
return errors.New("cfg.MapUsecase is required")
}
h := &handler{cfg.MapUsecase, cfg.ServerUsecase}
cfg.RouterGroup.GET("/map/:server", h.mapHandler)

View File

@ -2,7 +2,6 @@ package usecase
import (
"context"
"fmt"
"github.com/tribalwarshelp/shared/tw/twmodel"
"sort"
"strconv"
@ -194,14 +193,14 @@ func concatMarkers(slices ...[]*generator.Marker) []*generator.Marker {
func toMarker(param string) (int, string, error) {
splitted := strings.Split(param, ",")
if len(splitted) != 2 {
return 0, "", fmt.Errorf("%s: Invalid marker format (should be id,#hexcolor)", param)
return 0, "", errors.Errorf("%s: Invalid marker format (should be id,#hexcolor)", param)
}
id, err := strconv.Atoi(splitted[0])
if err != nil {
return 0, "", errors.Wrapf(err, "%s: Invalid marker format (should be id,#hexcolor)", param)
}
if id <= 0 {
return 0, "", fmt.Errorf("ID should be greater than 0")
return 0, "", errors.New("ID should be greater than 0")
}
return id, splitted[1], nil

View File

@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -45,9 +45,9 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg serverstats.FetchConfig
}
if err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
return nil, 0, fmt.Errorf("Server not found")
return nil, 0, errors.New("Server not found")
}
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -46,9 +46,9 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg tribe.FetchConfig) ([]*
}
if err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
return nil, 0, fmt.Errorf("Server not found")
return nil, 0, errors.New("Server not found")
}
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil
@ -62,7 +62,7 @@ func (repo *pgRepository) SearchTribe(ctx context.Context, cfg tribe.SearchTribe
Column("key").
Where("version_code = ?", cfg.Version).
Select(); err != nil {
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
var query *orm.Query
@ -102,7 +102,7 @@ func (repo *pgRepository) SearchTribe(ctx context.Context, cfg tribe.SearchTribe
err = base.Select(&res)
}
if err != nil && err != pg.ErrNoRows {
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
}

View File

@ -2,7 +2,7 @@ package usecase
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -42,17 +42,17 @@ func (ucase *usecase) GetByID(ctx context.Context, server string, id int) (*twmo
return nil, err
}
if len(tribes) == 0 {
return nil, fmt.Errorf("Tribe (ID: %s) not found.", id)
return nil, errors.Errorf("Tribe (ID: %d) not found.", id)
}
return tribes[0], nil
}
func (ucase *usecase) SearchTribe(ctx context.Context, cfg tribe.SearchTribeConfig) ([]*twmodel.FoundTribe, int, error) {
if "" == strings.TrimSpace(cfg.Version) {
return nil, 0, fmt.Errorf("Version is required.")
return nil, 0, errors.New("Version is required.")
}
if "" == strings.TrimSpace(cfg.Query) {
return nil, 0, fmt.Errorf("Your search is ambiguous. You must specify the variable 'query'.")
return nil, 0, errors.New("Query is too ambiguous. You must specify the variable 'query'.")
}
if !middleware.CanExceedLimit(ctx) && (cfg.Limit > tribe.FetchLimit || cfg.Limit <= 0) {
cfg.Limit = tribe.FetchLimit

View File

@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -45,9 +45,9 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg tribechange.FetchConfig
}
if err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
return nil, 0, fmt.Errorf("Server not found")
return nil, 0, errors.New("Server not found")
}
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -45,9 +45,9 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg tribehistory.FetchConfi
}
if err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
return nil, 0, fmt.Errorf("Server not found")
return nil, 0, errors.New("Server not found")
}
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -2,7 +2,6 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/tribalwarshelp/shared/tw/twmodel"
@ -49,7 +48,7 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg version.FetchConfig) ([
total, err = query.Count()
}
if err != nil && err != pg.ErrNoRows {
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -2,7 +2,7 @@ package usecase
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"github.com/tribalwarshelp/api/middleware"
@ -42,7 +42,7 @@ func (ucase *usecase) GetByCode(ctx context.Context, code twmodel.VersionCode) (
return nil, err
}
if len(versions) == 0 {
return nil, fmt.Errorf("There is no version with code: %s.", code)
return nil, errors.Errorf("version (Code: %s) not found", code)
}
return versions[0], nil
}

View File

@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"strings"
@ -48,9 +48,9 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg village.FetchConfig) ([
}
if err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
return nil, 0, fmt.Errorf("Server not found")
return nil, 0, errors.New("Server not found")
}
return nil, 0, fmt.Errorf("Internal server error")
return nil, 0, errors.New("Internal server error")
}
return data, total, nil

View File

@ -2,7 +2,7 @@ package usecase
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/tw/twmodel"
"github.com/tribalwarshelp/api/middleware"
@ -41,7 +41,7 @@ func (ucase *usecase) GetByID(ctx context.Context, server string, id int) (*twmo
return nil, err
}
if len(villages) == 0 {
return nil, fmt.Errorf("Village (ID: %d) not found.", id)
return nil, errors.Errorf("Village (ID: %d) not found.", id)
}
return villages[0], nil
}