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.
- '\bpath\.(Ext|Base|Dir|Join)'
# 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
# tester/suite.go instead. Use an ignore lower down to exclude packages
# 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 )?'
# 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:
line-length: 120
revive:
@ -120,6 +123,7 @@ issues:
- gofumpt
- misspell
- errcheck
- forbidigo
- path: internal/tester/suite.go
linters:
- forbidigo

View File

@ -2,7 +2,6 @@ package discovery
import (
"context"
"fmt"
"github.com/alcionai/clues"
"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)
if err != nil {
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")

View File

@ -279,7 +279,7 @@ func (c Contacts) Serialize(
) ([]byte, error) {
contact, ok := item.(models.Contactable)
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()))

View File

@ -314,7 +314,7 @@ func (c Events) Serialize(
) ([]byte, error) {
event, ok := item.(models.Eventable)
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()))

View File

@ -324,7 +324,7 @@ func (c Mail) Serialize(
) ([]byte, error) {
msg, ok := item.(models.Messageable)
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()))

View File

@ -3,6 +3,7 @@ package api
import (
"fmt"
"github.com/alcionai/clues"
abstractions "github.com/microsoft/kiota-abstractions-go"
"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 {
_, ok := allowed[entry]
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) {
gv, ok := a.(interface{ GetValue() []T })
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()
@ -47,7 +47,7 @@ func toValues[T any](a any) ([]getIDAndAddtler, error) {
ri, ok := a.(getIDAndAddtler)
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)

View File

@ -569,7 +569,7 @@ func CreateContainerDestination(
errs)
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"
"strings"
"github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/common/ptr"
@ -325,13 +326,13 @@ const (
func ToItemAttachment(orig models.Attachmentable) (models.Attachmentable, error) {
transform, ok := orig.(models.ItemAttachmentable)
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()
itemType := item.GetOdataType()
itemType := ptr.Val(item.GetOdataType())
switch *itemType {
switch itemType {
case contactItemType:
contact := item.(models.Contactable)
revised := sanitizeContact(contact)
@ -362,7 +363,7 @@ func ToItemAttachment(orig models.Attachmentable) (models.Attachmentable, error)
return transform, nil
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 (
"encoding/json"
"fmt"
"testing"
"github.com/alcionai/clues"
@ -388,7 +387,7 @@ func (suite *FaultErrorsUnitSuite) TestUnmarshalLegacy() {
t := suite.T()
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)

View File

@ -3,10 +3,10 @@ package selectors
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/alcionai/clues"
"github.com/pkg/errors"
"github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/fault"
@ -391,7 +391,7 @@ func pathComparator() option {
}
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 {