replace fmt.Errorf with errors.New/errors.Errorf
This commit is contained in:
parent
c3c57b780c
commit
0b14efd014
|
@ -1,7 +1,6 @@
|
|||
package jwt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
|
@ -63,7 +62,7 @@ func verifyToken(secret string, tokenString string) (*jwt.Token, error) {
|
|||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
//Make sure that the token method conform to "SigningMethodHMAC"
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(secret), nil
|
||||
})
|
||||
|
@ -71,7 +70,7 @@ func verifyToken(secret string, tokenString string) (*jwt.Token, error) {
|
|||
return nil, errors.Wrap(err, "verifyToken")
|
||||
}
|
||||
if !token.Valid {
|
||||
return nil, fmt.Errorf("Token is invalid")
|
||||
return nil, errors.New("Token is invalid")
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
@ -86,15 +85,15 @@ func extractTokenMetadata(secret, tokenString string) (*Metadata, error) {
|
|||
if ok {
|
||||
staySignedIn, ok := claims["stay_signed_in"].(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid token payload (staySignedIn should be a boolean)")
|
||||
return nil, errors.New("Invalid token payload (staySignedIn should be a boolean)")
|
||||
}
|
||||
email, ok := claims["email"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid token payload (email should be a string)")
|
||||
return nil, errors.New("Invalid token payload (email should be a string)")
|
||||
}
|
||||
password, ok := claims["password"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid token payload (password should be a string)")
|
||||
return nil, errors.New("Invalid token payload (password should be a string)")
|
||||
}
|
||||
return &Metadata{
|
||||
StaySignedIn: staySignedIn,
|
||||
|
@ -105,5 +104,5 @@ func extractTokenMetadata(secret, tokenString string) (*Metadata, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Cannot extract token metadata")
|
||||
return nil, errors.New("Cannot extract token metadata")
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package usecase
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/auth"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/auth/jwt"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
|
@ -22,7 +22,7 @@ type Config struct {
|
|||
|
||||
func New(cfg *Config) (auth.Usecase, error) {
|
||||
if cfg == nil || cfg.UserRepository == nil {
|
||||
return nil, fmt.Errorf("user/usecase: UserRepository is required")
|
||||
return nil, errors.New("user/usecase: UserRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
cfg.UserRepository,
|
||||
|
@ -31,7 +31,7 @@ func New(cfg *Config) (auth.Usecase, error) {
|
|||
}
|
||||
|
||||
func (ucase *usecase) SignIn(ctx context.Context, email, password string, staySignedIn bool) (*models.User, string, error) {
|
||||
user, err := ucase.GetUserByCredentials(ctx, email, password)
|
||||
u, err := ucase.GetUserByCredentials(ctx, email, password)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
@ -39,15 +39,15 @@ func (ucase *usecase) SignIn(ctx context.Context, email, password string, staySi
|
|||
token, err := ucase.tokenGenerator.Generate(jwt.Metadata{
|
||||
StaySignedIn: staySignedIn,
|
||||
Credentials: jwt.Credentials{
|
||||
Email: user.Email,
|
||||
Password: user.Password,
|
||||
Email: u.Email,
|
||||
Password: u.Password,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, "", errorutils.Wrap(err, messageInvalidCredentials)
|
||||
}
|
||||
|
||||
return user, token, nil
|
||||
return u, token, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*models.User, error) {
|
||||
|
@ -71,13 +71,13 @@ func (ucase *usecase) GetUserByCredentials(ctx context.Context, email, password
|
|||
return nil, err
|
||||
}
|
||||
if len(users) <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidCredentials)
|
||||
return nil, errors.New(messageInvalidCredentials)
|
||||
}
|
||||
|
||||
user := users[0]
|
||||
if err := user.CompareHashAndPassword(password); err != nil {
|
||||
u := users[0]
|
||||
if err := u.CompareHashAndPassword(password); err != nil {
|
||||
return nil, errorutils.Wrap(err, messageInvalidCredentials)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
return u, nil
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package middleware
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/auth"
|
||||
|
@ -35,13 +35,13 @@ func Authenticate(ucase auth.Usecase) gin.HandlerFunc {
|
|||
func UserFromContext(ctx context.Context) (*models.User, error) {
|
||||
user := ctx.Value(authenticateKey)
|
||||
if user == nil {
|
||||
err := fmt.Errorf("Could not retrieve *models.User")
|
||||
err := errors.New("Could not retrieve *models.User")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u, ok := user.(*models.User)
|
||||
if !ok {
|
||||
err := fmt.Errorf("*models.User has wrong type")
|
||||
err := errors.New("*models.User has wrong type")
|
||||
return nil, err
|
||||
}
|
||||
return u, nil
|
||||
|
|
|
@ -2,7 +2,7 @@ package middleware
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/dataloader"
|
||||
|
@ -23,13 +23,13 @@ func DataLoaderToContext(cfg dataloader.Config) gin.HandlerFunc {
|
|||
func DataLoaderFromContext(ctx context.Context) (*dataloader.DataLoader, error) {
|
||||
dataLoader := ctx.Value(dataLoaderToContext)
|
||||
if dataLoader == nil {
|
||||
err := fmt.Errorf("could not retrieve dataloader.DataLoader")
|
||||
err := errors.New("could not retrieve dataloader.DataLoader")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dl, ok := dataLoader.(*dataloader.DataLoader)
|
||||
if !ok {
|
||||
err := fmt.Errorf("dataloader.DataLoader has wrong type")
|
||||
err := errors.New("dataloader.DataLoader has wrong type")
|
||||
return nil, err
|
||||
}
|
||||
return dl, nil
|
||||
|
|
|
@ -2,7 +2,7 @@ package middleware
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
@ -22,13 +22,13 @@ func GinContextToContext() gin.HandlerFunc {
|
|||
func GinContextFromContext(ctx context.Context) (*gin.Context, error) {
|
||||
ginContext := ctx.Value(ginContextToContextKey)
|
||||
if ginContext == nil {
|
||||
err := fmt.Errorf("could not retrieve gin.Context")
|
||||
err := errors.New("could not retrieve gin.Context")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gc, ok := ginContext.(*gin.Context)
|
||||
if !ok {
|
||||
err := fmt.Errorf("gin.Context has wrong type")
|
||||
err := errors.New("gin.Context has wrong type")
|
||||
return nil, err
|
||||
}
|
||||
return gc, nil
|
||||
|
|
|
@ -2,6 +2,7 @@ package httpdelivery
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/querycomplexity"
|
||||
"time"
|
||||
|
||||
|
@ -30,7 +31,7 @@ type Config struct {
|
|||
|
||||
func Attach(group *gin.RouterGroup, cfg Config) error {
|
||||
if cfg.Resolver == nil {
|
||||
return fmt.Errorf("Graphql resolver cannot be nil")
|
||||
return errors.New("Graphql resolver cannot be nil")
|
||||
}
|
||||
gqlHandler := graphqlHandler(prepareConfig(cfg.Resolver, cfg.Directive))
|
||||
group.GET(graphqlEndpoint, gqlHandler)
|
||||
|
|
|
@ -2,7 +2,7 @@ package directive
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/gin/middleware"
|
||||
|
@ -17,7 +17,7 @@ func (d *Directive) Authenticated(ctx context.Context, obj interface{}, next gra
|
|||
if yes && err != nil {
|
||||
return nil, errorutils.Wrap(err, messageMustBeSignedIn)
|
||||
} else if !yes && err == nil {
|
||||
return nil, fmt.Errorf(messageMustBeSignedOut)
|
||||
return nil, errors.New(messageMustBeSignedOut)
|
||||
}
|
||||
|
||||
return next(ctx)
|
||||
|
@ -29,7 +29,7 @@ func (d *Directive) HasRole(ctx context.Context, obj interface{}, next graphql.R
|
|||
return nil, errorutils.Wrap(err, messageMustBeSignedIn)
|
||||
}
|
||||
if user.Role != role {
|
||||
return nil, fmt.Errorf(messageUnauthorized)
|
||||
return nil, errors.New(messageUnauthorized)
|
||||
}
|
||||
|
||||
return next(ctx)
|
||||
|
|
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -34,12 +35,12 @@ func (answer Answer) String() string {
|
|||
func (answer *Answer) UnmarshalGQL(v interface{}) error {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("enums must be strings")
|
||||
return errors.New("enums must be strings")
|
||||
}
|
||||
|
||||
*answer = Answer(strings.ToLower(str))
|
||||
if !answer.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid Answer", str)
|
||||
return errors.Errorf("%s is not a valid Answer", str)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -30,12 +31,12 @@ func (role Role) String() string {
|
|||
func (role *Role) UnmarshalGQL(v interface{}) error {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("enums must be strings")
|
||||
return errors.New("enums must be strings")
|
||||
}
|
||||
|
||||
*role = Role(strings.ToLower(str))
|
||||
if !role.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid Role", str)
|
||||
return errors.Errorf("%s is not a valid Role", str)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package repository
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
sqlutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/sql"
|
||||
"strings"
|
||||
|
||||
|
@ -23,7 +23,7 @@ type PGRepositoryConfig struct {
|
|||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (profession.Repository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, fmt.Errorf("profession/pg_repository: *pg.DB is required")
|
||||
return nil, errors.New("profession/pg_repository: *pg.DB is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
cfg.DB,
|
||||
|
@ -37,10 +37,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.ProfessionInp
|
|||
Context(ctx).
|
||||
Returning("*").
|
||||
Insert(); err != nil {
|
||||
if strings.Contains(err.Error(), "name") || strings.Contains(err.Error(), "slug") {
|
||||
return nil, errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
@ -52,10 +49,7 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.ProfessionFi
|
|||
Apply(input.ApplyUpdate).
|
||||
Apply(f.Where).
|
||||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
if strings.Contains(err.Error(), "name") || strings.Contains(err.Error(), "slug") {
|
||||
return nil, errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
items, _, err := repo.Fetch(ctx, &profession.FetchConfig{
|
||||
Count: false,
|
||||
|
@ -125,3 +119,10 @@ func (repo *pgRepository) GetAssociatedQualifications(
|
|||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func handleInsertAndUpdateError(err error) error {
|
||||
if strings.Contains(err.Error(), "name") || strings.Contains(err.Error(), "slug") {
|
||||
return errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
}
|
||||
return errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package usecase
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
|
@ -19,7 +19,7 @@ type Config struct {
|
|||
|
||||
func New(cfg *Config) (profession.Usecase, error) {
|
||||
if cfg == nil || cfg.ProfessionRepository == nil {
|
||||
return nil, fmt.Errorf("profession/usecase: ProfessionRepository is required")
|
||||
return nil, errors.New("profession/usecase: ProfessionRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
cfg.ProfessionRepository,
|
||||
|
@ -35,7 +35,7 @@ func (ucase *usecase) Store(ctx context.Context, input *models.ProfessionInput)
|
|||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.ProfessionInput) (*models.Profession, error) {
|
||||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
|
@ -49,7 +49,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.P
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*models.Profession,
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func (ucase *usecase) GetBySlug(ctx context.Context, slug string) (*models.Profe
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -109,17 +109,17 @@ type validateOptions struct {
|
|||
|
||||
func validateInput(input *models.ProfessionInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
||||
if input.Name != nil {
|
||||
if *input.Name == "" {
|
||||
return fmt.Errorf(messageNameIsRequired)
|
||||
return errors.New(messageNameIsRequired)
|
||||
} else if len(*input.Name) > profession.MaxNameLength {
|
||||
return fmt.Errorf(messageNameIsTooLong, profession.MaxNameLength)
|
||||
return errors.Errorf(messageNameIsTooLong, profession.MaxNameLength)
|
||||
}
|
||||
} else if !opts.allowNilValues {
|
||||
return fmt.Errorf(messageNameIsRequired)
|
||||
return errors.New(messageNameIsRequired)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -2,7 +2,7 @@ package repository
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
|
||||
sqlutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/sql"
|
||||
|
@ -24,7 +24,7 @@ type PGRepositoryConfig struct {
|
|||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (qualification.Repository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, fmt.Errorf("qualification/pg_repository: *pg.DB is required")
|
||||
return nil, errors.New("qualification/pg_repository: *pg.DB is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
cfg.DB,
|
||||
|
|
|
@ -2,7 +2,7 @@ package repository
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -28,7 +28,7 @@ type PGRepositoryConfig struct {
|
|||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (question.Repository, error) {
|
||||
if cfg == nil || cfg.DB == nil || cfg.FileStorage == nil {
|
||||
return nil, fmt.Errorf("question/pg_repository: *pg.DB and filestorage.FileStorage are required")
|
||||
return nil, errors.New("question/pg_repository: *pg.DB and filestorage.FileStorage are required")
|
||||
}
|
||||
return &pgRepository{
|
||||
cfg.DB,
|
||||
|
@ -47,10 +47,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.QuestionInput
|
|||
if _, err := baseQuery.
|
||||
Clone().
|
||||
Insert(); err != nil {
|
||||
if strings.Contains(err.Error(), "questions_from_content_correct_answer_qualification_id_key") {
|
||||
return nil, errorutils.Wrap(err, messageSimilarRecordExists)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
|
||||
repo.saveImages(item, input)
|
||||
|
@ -82,10 +79,7 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
Clone().
|
||||
Apply(input.ApplyUpdate).
|
||||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
if strings.Contains(err.Error(), "questions_from_content_correct_answer_qualification_id_key") {
|
||||
return nil, errorutils.Wrap(err, messageSimilarRecordExists)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
|
||||
repo.saveImages(item, input)
|
||||
|
@ -99,7 +93,7 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
Set("answer_c_image = ?", item.AnswerCImage).
|
||||
Set("answer_d_image = ?", item.AnswerDImage).
|
||||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
|
||||
return item, nil
|
||||
|
@ -166,3 +160,10 @@ func (repo *pgRepository) GenerateTest(ctx context.Context, cfg *question.Genera
|
|||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func handleInsertAndUpdateError(err error) error {
|
||||
if strings.Contains(err.Error(), "questions_from_content_correct_answer_qualification_id_key") {
|
||||
return errorutils.Wrap(err, messageSimilarRecordExists)
|
||||
}
|
||||
return errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package usecase
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
||||
sqlutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/sql"
|
||||
|
@ -26,7 +26,7 @@ type Config struct {
|
|||
|
||||
func New(cfg *Config) (question.Usecase, error) {
|
||||
if cfg == nil || cfg.QuestionRepository == nil {
|
||||
return nil, fmt.Errorf("question/usecase: cfg.QuestionRepository is required")
|
||||
return nil, errors.New("question/usecase: cfg.QuestionRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
cfg.QuestionRepository,
|
||||
|
@ -42,7 +42,7 @@ func (ucase *usecase) Store(ctx context.Context, input *models.QuestionInput) (*
|
|||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.QuestionInput) (*models.Question, error) {
|
||||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
|
@ -54,7 +54,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.Q
|
|||
return nil, err
|
||||
}
|
||||
if item == nil {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*models.Question, er
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -112,119 +112,119 @@ type validateOptions struct {
|
|||
|
||||
func validateInput(input *models.QuestionInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
||||
if input.Content != nil {
|
||||
if *input.Content == "" {
|
||||
return fmt.Errorf(messageContentIsRequired)
|
||||
return errors.New(messageContentIsRequired)
|
||||
}
|
||||
} else if !opts.allowNilValues {
|
||||
return fmt.Errorf(messageContentIsRequired)
|
||||
return errors.New(messageContentIsRequired)
|
||||
}
|
||||
|
||||
if input.CorrectAnswer != nil {
|
||||
if !input.CorrectAnswer.IsValid() {
|
||||
return fmt.Errorf(messageCorrectAnswerIsInvalid)
|
||||
return errors.New(messageCorrectAnswerIsInvalid)
|
||||
}
|
||||
} else if !opts.allowNilValues {
|
||||
return fmt.Errorf(messageCorrectAnswerIsInvalid)
|
||||
return errors.New(messageCorrectAnswerIsInvalid)
|
||||
}
|
||||
|
||||
if input.QualificationID != nil {
|
||||
if *input.QualificationID <= 0 {
|
||||
return fmt.Errorf(messageQualificationIDIsRequired)
|
||||
return errors.New(messageQualificationIDIsRequired)
|
||||
}
|
||||
} else if !opts.allowNilValues {
|
||||
return fmt.Errorf(messageQualificationIDIsRequired)
|
||||
return errors.New(messageQualificationIDIsRequired)
|
||||
}
|
||||
|
||||
if input.AnswerA != nil {
|
||||
if *input.AnswerA == "" {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "A")
|
||||
return errors.Errorf(messageAnswerIsRequired, "A")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerB != nil {
|
||||
if *input.AnswerA == "" {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "B")
|
||||
return errors.Errorf(messageAnswerIsRequired, "B")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerC != nil {
|
||||
if *input.AnswerC == "" {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "C")
|
||||
return errors.Errorf(messageAnswerIsRequired, "C")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerD != nil {
|
||||
if *input.AnswerD == "" {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "D")
|
||||
return errors.Errorf(messageAnswerIsRequired, "D")
|
||||
}
|
||||
}
|
||||
|
||||
if input.Image != nil {
|
||||
if !isValidMIMEType(input.Image.ContentType) {
|
||||
return fmt.Errorf(messageImageNotAcceptableMIMEType, "Obrazek pytanie")
|
||||
return errors.Errorf(messageImageNotAcceptableMIMEType, "Obrazek pytanie")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerAImage != nil {
|
||||
if !isValidMIMEType(input.AnswerAImage.ContentType) {
|
||||
return fmt.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź A")
|
||||
return errors.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź A")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerBImage != nil {
|
||||
if !isValidMIMEType(input.AnswerBImage.ContentType) {
|
||||
return fmt.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź B")
|
||||
return errors.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź B")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerCImage != nil {
|
||||
if !isValidMIMEType(input.AnswerCImage.ContentType) {
|
||||
return fmt.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź C")
|
||||
return errors.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź C")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerDImage != nil {
|
||||
if !isValidMIMEType(input.AnswerDImage.ContentType) {
|
||||
return fmt.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź D")
|
||||
return errors.Errorf(messageImageNotAcceptableMIMEType, "Obrazek odpowiedź D")
|
||||
}
|
||||
}
|
||||
|
||||
if input.DeleteAnswerAImage != nil && input.AnswerA == nil && input.AnswerAImage == nil {
|
||||
return fmt.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź A")
|
||||
return errors.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź A")
|
||||
}
|
||||
|
||||
if input.DeleteAnswerBImage != nil && input.AnswerB == nil && input.AnswerBImage == nil {
|
||||
return fmt.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź B")
|
||||
return errors.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź B")
|
||||
}
|
||||
|
||||
if input.DeleteAnswerCImage != nil && input.AnswerC == nil && input.AnswerCImage == nil {
|
||||
return fmt.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź C")
|
||||
return errors.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź C")
|
||||
}
|
||||
|
||||
if input.DeleteAnswerDImage != nil && input.AnswerD == nil && input.AnswerDImage == nil {
|
||||
return fmt.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź D")
|
||||
return errors.Errorf(messageCannotDeleteImageWithoutNewAnswer, "Obrazek odpowiedź D")
|
||||
|
||||
}
|
||||
|
||||
if !opts.allowNilValues {
|
||||
if input.AnswerA == nil && input.AnswerAImage == nil {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "A")
|
||||
return errors.Errorf(messageAnswerIsRequired, "A")
|
||||
}
|
||||
|
||||
if input.AnswerB == nil && input.AnswerBImage == nil {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "B")
|
||||
return errors.Errorf(messageAnswerIsRequired, "B")
|
||||
}
|
||||
|
||||
if input.AnswerC == nil && input.AnswerCImage == nil {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "C")
|
||||
return errors.Errorf(messageAnswerIsRequired, "C")
|
||||
}
|
||||
|
||||
if input.AnswerD == nil && input.AnswerDImage == nil {
|
||||
return fmt.Errorf(messageAnswerIsRequired, "D")
|
||||
return errors.Errorf(messageAnswerIsRequired, "D")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package repository
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
|
||||
errorutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/error"
|
||||
|
@ -22,7 +22,7 @@ type PGRepositoryConfig struct {
|
|||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (user.Repository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, fmt.Errorf("user/pg_repository: *pg.DB is required")
|
||||
return nil, errors.New("user/pg_repository: *pg.DB is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
cfg.DB,
|
||||
|
@ -36,10 +36,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.UserInput) (*
|
|||
Context(ctx).
|
||||
Returning("*").
|
||||
Insert(); err != nil {
|
||||
if strings.Contains(err.Error(), "email") {
|
||||
return nil, errorutils.Wrap(err, messageEmailIsAlreadyTaken)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
@ -51,10 +48,7 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.UserFilter,
|
|||
Apply(input.ApplyUpdate).
|
||||
Apply(f.Where).
|
||||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
if strings.Contains(err.Error(), "email") {
|
||||
return nil, errorutils.Wrap(err, messageEmailIsAlreadyTaken)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
return nil, handleInsertAndUpdateError(err)
|
||||
}
|
||||
items, _, err := repo.Fetch(ctx, &user.FetchConfig{
|
||||
Count: false,
|
||||
|
@ -101,3 +95,10 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*
|
|||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
func handleInsertAndUpdateError(err error) error {
|
||||
if strings.Contains(err.Error(), "email") {
|
||||
return errorutils.Wrap(err, messageEmailIsAlreadyTaken)
|
||||
}
|
||||
return errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package usecase
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
|
@ -20,7 +20,7 @@ type Config struct {
|
|||
|
||||
func New(cfg *Config) (user.Usecase, error) {
|
||||
if cfg == nil || cfg.UserRepository == nil {
|
||||
return nil, fmt.Errorf("user/usecase: UserRepository is required")
|
||||
return nil, errors.New("user/usecase: UserRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
cfg.UserRepository,
|
||||
|
@ -36,7 +36,7 @@ func (ucase *usecase) Store(ctx context.Context, input *models.UserInput) (*mode
|
|||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.UserInput) (*models.User, error) {
|
||||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
items, err := ucase.UpdateMany(
|
||||
ctx,
|
||||
|
@ -49,7 +49,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.U
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*models.User, error)
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageItemNotFound)
|
||||
return nil, errors.New(messageItemNotFound)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -115,10 +115,10 @@ func (ucase *usecase) GetByCredentials(ctx context.Context, email, password stri
|
|||
return nil, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf(messageInvalidCredentials)
|
||||
return nil, errors.New(messageInvalidCredentials)
|
||||
}
|
||||
if err := items[0].CompareHashAndPassword(password); err != nil {
|
||||
return nil, fmt.Errorf(messageInvalidCredentials)
|
||||
return nil, errors.New(messageInvalidCredentials)
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
|
@ -129,43 +129,43 @@ type validateOptions struct {
|
|||
|
||||
func validateInput(input *models.UserInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
||||
if input.DisplayName != nil {
|
||||
displayNameLength := len(*input.DisplayName)
|
||||
if displayNameLength < user.MinDisplayNameLength {
|
||||
return fmt.Errorf(messageDisplayNameIsRequired)
|
||||
return errors.New(messageDisplayNameIsRequired)
|
||||
} else if displayNameLength > user.MaxDisplayNameLength {
|
||||
return fmt.Errorf(messageDisplayNameIsTooLong, user.MaxDisplayNameLength)
|
||||
return errors.Errorf(messageDisplayNameIsTooLong, user.MaxDisplayNameLength)
|
||||
}
|
||||
} else if !opts.acceptNilValues {
|
||||
return fmt.Errorf(messageDisplayNameIsRequired)
|
||||
return errors.New(messageDisplayNameIsRequired)
|
||||
}
|
||||
|
||||
if input.Email != nil {
|
||||
if !utils.IsEmailValid(*input.Email) {
|
||||
return fmt.Errorf(messageEmailIsInvalid)
|
||||
return errors.New(messageEmailIsInvalid)
|
||||
}
|
||||
} else if !opts.acceptNilValues {
|
||||
return fmt.Errorf(messageEmailIsRequired)
|
||||
return errors.New(messageEmailIsRequired)
|
||||
}
|
||||
|
||||
if input.Password != nil {
|
||||
passwordLength := len(*input.Password)
|
||||
if passwordLength > user.MaxPasswordLength || passwordLength < user.MinPasswordLength {
|
||||
return fmt.Errorf(messagePasswordInvalidLength, user.MinPasswordLength, user.MaxPasswordLength)
|
||||
return errors.Errorf(messagePasswordInvalidLength, user.MinPasswordLength, user.MaxPasswordLength)
|
||||
}
|
||||
} else if !opts.acceptNilValues {
|
||||
return fmt.Errorf(messagePasswordIsRequired)
|
||||
return errors.New(messagePasswordIsRequired)
|
||||
}
|
||||
|
||||
if input.Role != nil {
|
||||
if !input.Role.IsValid() {
|
||||
return fmt.Errorf(messageInvalidRole)
|
||||
return errors.New(messageInvalidRole)
|
||||
}
|
||||
} else if !opts.acceptNilValues {
|
||||
return fmt.Errorf(messageInvalidRole)
|
||||
return errors.New(messageInvalidRole)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
)
|
||||
|
||||
func Wrap(details error, message string) error {
|
||||
return Wrapf(details, message)
|
||||
return errors.Wrap(details, message)
|
||||
}
|
||||
|
||||
func Wrapf(details error, message string, args ...interface{}) error {
|
||||
|
|
Reference in New Issue