add linter for using clues to create and wrap errs (#2959)

#### Does this PR need a docs update or release note?

- [x]  No

#### Type of change

- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #1970

#### Test Plan

- [x]  Unit test
This commit is contained in:
Keepers 2023-03-29 15:16:05 -06:00 committed by GitHub
parent ee9a1013bc
commit e09c120778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 22 additions and 18 deletions

View File

@ -34,11 +34,14 @@ linters-settings:
# Use filepath instead. # Use filepath instead.
- '\bpath\.(Ext|Base|Dir|Join)' - '\bpath\.(Ext|Base|Dir|Join)'
# Don't allow the typo m356 to be used in place of m365. # Don't allow the typo m356 to be used in place of m365.
- '[Mm]356' - '[Mm]356(# typo: should be 365)?'
# Don't allow use of testify suite directly. Use one of the wrappers from # Don't allow use of testify suite directly. Use one of the wrappers from
# tester/suite.go instead. Use an ignore lower down to exclude packages # tester/suite.go instead. Use an ignore lower down to exclude packages
# that result in import cycles if they try to use the wrapper. # that result in import cycles if they try to use the wrapper.
- 'suite\.Suite(# tests should use one of the Suite wrappers in tester package )?' - 'suite\.Suite(# tests should use one of the Suite wrappers in tester package )?'
# All errors should be constructed and wrapped with the clues package.
# String formatting should be avoided in favor of structured errors (ie: err.With(k, v)).
- '(errors|fmt)\.(New|Stack|Wrap|Error)f?\((# error handling should use clues pkg)?'
lll: lll:
line-length: 120 line-length: 120
revive: revive:
@ -120,6 +123,7 @@ issues:
- gofumpt - gofumpt
- misspell - misspell
- errcheck - errcheck
- forbidigo
- path: internal/tester/suite.go - path: internal/tester/suite.go
linters: linters:
- forbidigo - forbidigo

View File

@ -2,7 +2,6 @@ package discovery
import ( import (
"context" "context"
"fmt"
"github.com/alcionai/clues" "github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
@ -57,7 +56,7 @@ func User(ctx context.Context, gwi getWithInfoer, userID string) (models.Userabl
u, err := gwi.GetByID(ctx, userID) u, err := gwi.GetByID(ctx, userID)
if err != nil { if err != nil {
if graph.IsErrUserNotFound(err) { if graph.IsErrUserNotFound(err) {
return nil, nil, fmt.Errorf("resource owner [%s] not found within tenant", userID) return nil, nil, clues.New("resource owner not found within tenant").With("user_id", userID)
} }
return nil, nil, clues.Wrap(err, "getting user") return nil, nil, clues.Wrap(err, "getting user")

View File

@ -279,7 +279,7 @@ func (c Contacts) Serialize(
) ([]byte, error) { ) ([]byte, error) {
contact, ok := item.(models.Contactable) contact, ok := item.(models.Contactable)
if !ok { if !ok {
return nil, clues.Wrap(fmt.Errorf("parseable type: %T", item), "parsable is not a Contactable") return nil, clues.New(fmt.Sprintf("item is not a Contactable: %T", item))
} }
ctx = clues.Add(ctx, "item_id", ptr.Val(contact.GetId())) ctx = clues.Add(ctx, "item_id", ptr.Val(contact.GetId()))

View File

@ -314,7 +314,7 @@ func (c Events) Serialize(
) ([]byte, error) { ) ([]byte, error) {
event, ok := item.(models.Eventable) event, ok := item.(models.Eventable)
if !ok { if !ok {
return nil, clues.Wrap(fmt.Errorf("parseable type: %T", item), "parsable is not an Eventable") return nil, clues.New(fmt.Sprintf("item is not an Eventable: %T", item))
} }
ctx = clues.Add(ctx, "item_id", ptr.Val(event.GetId())) ctx = clues.Add(ctx, "item_id", ptr.Val(event.GetId()))

View File

@ -324,7 +324,7 @@ func (c Mail) Serialize(
) ([]byte, error) { ) ([]byte, error) {
msg, ok := item.(models.Messageable) msg, ok := item.(models.Messageable)
if !ok { if !ok {
return nil, clues.Wrap(fmt.Errorf("parseable type: %T", item), "parsable is not a Messageable") return nil, clues.New(fmt.Sprintf("item is not a Messageable: %T", item))
} }
ctx = clues.Add(ctx, "item_id", ptr.Val(msg.GetId())) ctx = clues.Add(ctx, "item_id", ptr.Val(msg.GetId()))

View File

@ -3,6 +3,7 @@ package api
import ( import (
"fmt" "fmt"
"github.com/alcionai/clues"
abstractions "github.com/microsoft/kiota-abstractions-go" abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/microsoftgraph/msgraph-sdk-go/users" "github.com/microsoftgraph/msgraph-sdk-go/users"
) )
@ -262,7 +263,7 @@ func buildOptions(fields []string, allowed map[string]struct{}) ([]string, error
for _, entry := range fields { for _, entry := range fields {
_, ok := allowed[entry] _, ok := allowed[entry]
if !ok { if !ok {
return nil, fmt.Errorf("unsupported field: %v", entry) return nil, clues.New("unsupported field: " + entry)
} }
} }

View File

@ -36,7 +36,7 @@ type getIDAndAddtler interface {
func toValues[T any](a any) ([]getIDAndAddtler, error) { func toValues[T any](a any) ([]getIDAndAddtler, error) {
gv, ok := a.(interface{ GetValue() []T }) gv, ok := a.(interface{ GetValue() []T })
if !ok { if !ok {
return nil, clues.Wrap(fmt.Errorf("%T", a), "does not comply with the GetValue() interface") return nil, clues.New(fmt.Sprintf("type does not comply with the GetValue() interface: %T", a))
} }
items := gv.GetValue() items := gv.GetValue()
@ -47,7 +47,7 @@ func toValues[T any](a any) ([]getIDAndAddtler, error) {
ri, ok := a.(getIDAndAddtler) ri, ok := a.(getIDAndAddtler)
if !ok { if !ok {
return nil, clues.Wrap(fmt.Errorf("%T", item), "does not comply with the getIDAndAddtler interface") return nil, clues.New(fmt.Sprintf("type does not comply with the getIDAndAddtler interface: %T", item))
} }
r = append(r, ri) r = append(r, ri)

View File

@ -569,7 +569,7 @@ func CreateContainerDestination(
errs) errs)
default: default:
return "", clues.Wrap(fmt.Errorf("%T", category), "not support for exchange cache").WithClues(ctx) return "", clues.New(fmt.Sprintf("type not supported: %T", category)).WithClues(ctx)
} }
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/common/ptr" "github.com/alcionai/corso/src/internal/common/ptr"
@ -325,13 +326,13 @@ const (
func ToItemAttachment(orig models.Attachmentable) (models.Attachmentable, error) { func ToItemAttachment(orig models.Attachmentable) (models.Attachmentable, error) {
transform, ok := orig.(models.ItemAttachmentable) transform, ok := orig.(models.ItemAttachmentable)
if !ok { // Shouldn't ever happen if !ok { // Shouldn't ever happen
return nil, fmt.Errorf("transforming attachment to item attachment") return nil, clues.New("transforming attachment to item attachment")
} }
item := transform.GetItem() item := transform.GetItem()
itemType := item.GetOdataType() itemType := ptr.Val(item.GetOdataType())
switch *itemType { switch itemType {
case contactItemType: case contactItemType:
contact := item.(models.Contactable) contact := item.(models.Contactable)
revised := sanitizeContact(contact) revised := sanitizeContact(contact)
@ -362,7 +363,7 @@ func ToItemAttachment(orig models.Attachmentable) (models.Attachmentable, error)
return transform, nil return transform, nil
default: default:
return nil, fmt.Errorf("exiting ToItemAttachment: %s not supported", *itemType) return nil, clues.New(fmt.Sprintf("unsupported attachment type: %T", itemType))
} }
} }

View File

@ -2,7 +2,6 @@ package fault_test
import ( import (
"encoding/json" "encoding/json"
"fmt"
"testing" "testing"
"github.com/alcionai/clues" "github.com/alcionai/clues"
@ -388,7 +387,7 @@ func (suite *FaultErrorsUnitSuite) TestUnmarshalLegacy() {
t := suite.T() t := suite.T()
oldData := &legacyErrorsData{ oldData := &legacyErrorsData{
Errs: []error{fmt.Errorf("foo error"), fmt.Errorf("foo error"), fmt.Errorf("foo error")}, Errs: []error{clues.New("foo error"), clues.New("foo error"), clues.New("foo error")},
} }
jsonStr, err := json.Marshal(oldData) jsonStr, err := json.Marshal(oldData)

View File

@ -3,10 +3,10 @@ package selectors
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"strings" "strings"
"github.com/alcionai/clues" "github.com/alcionai/clues"
"github.com/pkg/errors"
"github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/fault"
@ -391,7 +391,7 @@ func pathComparator() option {
} }
func badCastErr(cast, is service) error { func badCastErr(cast, is service) error {
return clues.Stack(ErrorBadSelectorCast, errors.Errorf("%s is not %s", cast, is)) return clues.Stack(ErrorBadSelectorCast, clues.New(fmt.Sprintf("%s is not %s", cast, is)))
} }
func join(s ...string) string { func join(s ...string) string {