Skip to content

Commit

Permalink
Fix all remaining linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hlubek committed Jul 20, 2024
1 parent c2a6de6 commit 0249961
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 14 deletions.
5 changes: 3 additions & 2 deletions backend/finder/account_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package finder

import (
"context"
"errors"

"github.com/friendsofgo/errors"

"myvendor.mytld/myproject/backend/domain"
"myvendor.mytld/myproject/backend/persistence/repository"
Expand Down Expand Up @@ -31,7 +32,7 @@ func (f *Finder) QueryAccountNotAuthorized(ctx context.Context, query domain.Acc
return repository.FindAccountByEmailAddress(ctx, f.executor, *query.EmailAddress, query.Opts)
}

return domain.Account{}, errors.New("invalid query")
return domain.Account{}, errors.Wrap(ErrInvalidQuery, "AccountID or EmailAddress must be set")
}

func (f *Finder) QueryAccounts(ctx context.Context, query domain.AccountsQuery, paging Paging) ([]domain.Account, error) {
Expand Down
5 changes: 5 additions & 0 deletions backend/finder/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package finder

import "errors"

var ErrInvalidQuery = errors.New("invalid query")
5 changes: 4 additions & 1 deletion backend/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package finder
import (
"context"
"database/sql"
std_errors "errors"

"github.com/friendsofgo/errors"
"github.com/networkteam/qrb/qrbsql"
Expand All @@ -25,10 +26,12 @@ func NewFinder(db *sql.DB, timeSource domain.TimeSource) *Finder {
}
}

var errTransactionalNoSQLDB = std_errors.New("finder: executor for Transactional must be a *sql.DB")

func (f *Finder) Transactional(ctx context.Context, isolationLevel sql.IsolationLevel, callback func(txFinder *Finder) error) error {
db, ok := f.executor.(*sql.DB)
if !ok {
return errors.New("finder: executor for Transactional must be a *sql.DB")
return errors.WithStack(errTransactionalNoSQLDB)
}

return repository.TransactionalWithOpts(ctx, db, &sql.TxOptions{
Expand Down
10 changes: 8 additions & 2 deletions backend/mail/fixture/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fixture
import (
"bytes"
"context"
std_errors "errors"

"github.com/apex/log"
"github.com/friendsofgo/errors"
Expand All @@ -23,19 +24,24 @@ func NewSender() *Sender {

var _ mail.Sender = &Sender{}

var (
errNoSenderSet = std_errors.New("no sender set")
errNoRecipientSet = std_errors.New("no recipient set")
)

func (m *Sender) Send(_ context.Context, message *gomail.Msg) error {
if m.SendCallback != nil {
defer m.SendCallback(message)
}

fromHeaders := message.GetFromString()
if len(fromHeaders) == 0 {
return errors.New("no sender set")
return errors.WithStack(errNoSenderSet)
}

toHeaders := message.GetToString()
if len(toHeaders) == 0 {
return errors.New("no recipient(s) set")
return errors.WithStack(errNoRecipientSet)
}

var buf = new(bytes.Buffer)
Expand Down
7 changes: 5 additions & 2 deletions backend/mail/smtp/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package smtp

import (
"context"
std_errors "errors"

"github.com/friendsofgo/errors"
gomail "github.com/wneessen/go-mail"
Expand All @@ -18,7 +19,7 @@ var _ mail.Sender = new(sender)
func NewSender(host string, port int, username, password, tlsPolicy string) (mail.Sender, error) {
policy, err := getTLSPolicy(tlsPolicy)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

client, err := gomail.NewClient(
Expand Down Expand Up @@ -50,6 +51,8 @@ func (m *sender) Send(ctx context.Context, message *gomail.Msg) error {
return nil
}

var errInvalidTLSPolicy = std_errors.New("invalid TLS policy")

func getTLSPolicy(tlsPolicy string) (gomail.TLSPolicy, error) {
switch tlsPolicy {
case "opportunistic":
Expand All @@ -59,5 +62,5 @@ func getTLSPolicy(tlsPolicy string) (gomail.TLSPolicy, error) {
case "non":
return gomail.NoTLS, nil
}
return -1, errors.New("invalid TLS policy")
return -1, errInvalidTLSPolicy
}
6 changes: 4 additions & 2 deletions backend/persistence/repository/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository
import (
"context"
"database/sql"
"fmt"
std_errors "errors"
"strings"

"github.com/friendsofgo/errors"
Expand Down Expand Up @@ -81,13 +81,15 @@ func TransactionalWithOpts(ctx context.Context, proxy TxBeginner, opts *sql.TxOp

// --- Paging options

var ErrInvalidSortField = std_errors.New("invalid sort field")

type PagingOption func(query builder.SelectBuilder, sortFieldMapping map[string]builder.IdentExp) (builder.SelectBuilder, error)

func WithSort(field, order string) PagingOption {
return func(query builder.SelectBuilder, sortFieldMapping map[string]builder.IdentExp) (builder.SelectBuilder, error) {
col, ok := sortFieldMapping[strings.ToLower(field)]
if !ok {
return query, fmt.Errorf("invalid sort field: %s", field)
return query, errors.Wrap(ErrInvalidSortField, field)
}

orderByBuilder := query.OrderBy(col)
Expand Down
4 changes: 2 additions & 2 deletions backend/persistence/repository/organisation_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func DeleteOrganisation(ctx context.Context, executor qrbsql.Executor, id uuid.U
)
}

func OrganisationConstraintErr(err error) error {
func OrganisationConstraintErr(error) error {
return nil
}

func buildOrganisationJSON(opts domain.OrganisationQueryOpts) builder.JsonBuildObjectBuilder {
func buildOrganisationJSON(domain.OrganisationQueryOpts) builder.JsonBuildObjectBuilder {
return organisationDefaultJson
}
3 changes: 3 additions & 0 deletions backend/security/authorization/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (a *Authorizer) requireRole(anyOfRoles ...domain.Role) error {
return authorizationError{fmt.Sprintf("requires role in %s", strings.Join(roleIdentifiers, ","))}
}

//nolint:unused // Prepared for future use
func (a *Authorizer) requireSameAccount(accountID *uuid.UUID) error {
if !a.isSameAccount(accountID) {
return authorizationError{"requires to be same account"}
Expand All @@ -59,6 +60,7 @@ func (a *Authorizer) requireSameAccount(accountID *uuid.UUID) error {
return nil
}

//nolint:unused // Prepared for future use
func (a *Authorizer) isSameAccount(accountID *uuid.UUID) bool {
if a.authCtx.AccountID == uuid.Nil {
return false
Expand Down Expand Up @@ -103,6 +105,7 @@ func (a *Authorizer) requireOrganisationAdministrator(organisationID *uuid.UUID)
return a.requireSameOrganisation(organisationID)
}

//nolint:unused // Prepared for future use
func (a *Authorizer) requireOrganisationRole(organisationID *uuid.UUID) error {
if err := a.requireRole(domain.OrganisationRoles...); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions backend/security/authorization/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (a *Authorizer) AllowsAccountDeleteCmd(cmd domain.AccountDeleteCmd) error {
return a.requireOrganisationAdministrator(uuidOrNil(cmd.OrganisationID))
}

func (a *Authorizer) AllowsOrganisationCreateCmd(cmd domain.OrganisationCreateCmd) error {
func (a *Authorizer) AllowsOrganisationCreateCmd(domain.OrganisationCreateCmd) error {
return a.requireRole(domain.RoleSystemAdministrator)
}

func (a *Authorizer) AllowsOrganisationUpdateCmd(cmd domain.OrganisationUpdateCmd) error {
func (a *Authorizer) AllowsOrganisationUpdateCmd(domain.OrganisationUpdateCmd) error {
return a.requireRole(domain.RoleSystemAdministrator)
}

func (a *Authorizer) AllowsOrganisationDeleteCmd(cmd domain.OrganisationDeleteCmd) error {
func (a *Authorizer) AllowsOrganisationDeleteCmd(domain.OrganisationDeleteCmd) error {
return a.requireRole(domain.RoleSystemAdministrator)
}

0 comments on commit 0249961

Please sign in to comment.