Skip to content

Commit

Permalink
ci: Add error message format check (#901)
Browse files Browse the repository at this point in the history
Relevant issue(s)
Resolves #161

Description
This PR adds a linter rule that checks for proper formatting of the error message.
  • Loading branch information
fredcarle authored Oct 20, 2022
1 parent 574d853 commit f96fe72
Show file tree
Hide file tree
Showing 59 changed files with 179 additions and 152 deletions.
6 changes: 3 additions & 3 deletions api/http/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestHandleErrOnDefault(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("Unauthorized"), http.StatusUnauthorized)
handleErr(req.Context(), rw, errors.New("unauthorized"), http.StatusUnauthorized)
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
Expand All @@ -164,6 +164,6 @@ func TestHandleErrOnDefault(t *testing.T) {

assert.Equal(t, http.StatusUnauthorized, errResponse.Errors[0].Extensions.Status)
assert.Equal(t, http.StatusText(http.StatusUnauthorized), errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "Unauthorized", errResponse.Errors[0].Message)
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "[DEV] Unauthorized")
assert.Equal(t, "unauthorized", errResponse.Errors[0].Message)
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "[DEV] unauthorized")
}
4 changes: 2 additions & 2 deletions client/dockey.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewDocKeyV0(dataCID cid.Cid) DocKey {
func NewDocKeyFromString(key string) (DocKey, error) {
parts := strings.SplitN(key, "-", 2)
if len(parts) != 2 {
return DocKey{}, errors.New("Malformed DocKey, missing either version or cid")
return DocKey{}, errors.New("malformed DocKey, missing either version or cid")
}
versionStr := parts[0]
_, data, err := mbase.Decode(versionStr)
Expand All @@ -69,7 +69,7 @@ func NewDocKeyFromString(key string) (DocKey, error) {
return DocKey{}, err
}
if _, ok := ValidDocKeyVersions[uint16(version)]; !ok {
return DocKey{}, errors.New("Invalid DocKey version")
return DocKey{}, errors.New("invalid DocKey version")
}

uuid, err := uuid.FromString(parts[1])
Expand Down
2 changes: 1 addition & 1 deletion client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewDocFromMap(data map[string]any) (*Document, error) {
delete(data, "_key") // remove the key so it isn't parsed further
kstr, ok := k.(string)
if !ok {
return nil, errors.New("Provided _key in document must be a string type")
return nil, errors.New("provided _key in document must be a string type")
}
if doc.key, err = NewDocKeyFromString(kstr); err != nil {
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const (
var (
ErrFieldNotExist = errors.New(errFieldNotExist)
ErrSelectOfNonGroupField = errors.New(errSelectOfNonGroupField)
ErrFieldNotObject = errors.New("Trying to access field on a non object type")
ErrValueTypeMismatch = errors.New("Value does not match indicated type")
ErrIndexNotFound = errors.New("No index found for given ID")
ErrDocumentNotFound = errors.New("No document for the given key exists")
ErrInvalidUpdateTarget = errors.New("The target document to update is of invalid type")
ErrInvalidUpdater = errors.New("The updater of a document is of invalid type")
ErrInvalidDeleteTarget = errors.New("The target document to delete is of invalid type")
ErrFieldNotObject = errors.New("trying to access field on a non object type")
ErrValueTypeMismatch = errors.New("value does not match indicated type")
ErrIndexNotFound = errors.New("no index found for given ID")
ErrDocumentNotFound = errors.New("no document for the given key exists")
ErrInvalidUpdateTarget = errors.New("the target document to update is of invalid type")
ErrInvalidUpdater = errors.New("the updater of a document is of invalid type")
ErrInvalidDeleteTarget = errors.New("the target document to delete is of invalid type")
)

func NewErrFieldNotExist(name string) error {
Expand Down
2 changes: 1 addition & 1 deletion core/crdt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c CompositeDAG) DeltaDecode(node ipld.Node) (core.Delta, error) {
delta := &CompositeDAGDelta{}
pbNode, ok := node.(*dag.ProtoNode)
if !ok {
return nil, errors.New("Failed to cast ipld.Node to ProtoNode")
return nil, errors.New("failed to cast ipld.Node to ProtoNode")
}
data := pbNode.Data()
h := &codec.CborHandle{}
Expand Down
6 changes: 3 additions & 3 deletions core/crdt/lwwreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (reg LWWRegister) Merge(ctx context.Context, delta core.Delta, id string) e
func (reg LWWRegister) setValue(ctx context.Context, val []byte, priority uint64) error {
curPrio, err := reg.getPriority(ctx, reg.key)
if err != nil {
return errors.Wrap("Failed to get priority for Set ", err)
return errors.Wrap("failed to get priority for Set ", err)
}

// if the current priority is higher ignore put
Expand All @@ -159,7 +159,7 @@ func (reg LWWRegister) setValue(ctx context.Context, val []byte, priority uint64
buf := append([]byte{byte(client.LWW_REGISTER)}, val...)
err = reg.store.Put(ctx, valueK.ToDS(), buf)
if err != nil {
return errors.Wrap("Failed to store new value ", err)
return errors.Wrap("failed to store new value ", err)
}

return reg.setPriority(ctx, reg.key, priority)
Expand All @@ -172,7 +172,7 @@ func (reg LWWRegister) DeltaDecode(node ipld.Node) (core.Delta, error) {
delta := &LWWRegDelta{}
pbNode, ok := node.(*dag.ProtoNode)
if !ok {
return nil, errors.New("Failed to cast ipld.Node to ProtoNode")
return nil, errors.New("failed to cast ipld.Node to ProtoNode")
}
data := pbNode.Data()
h := &codec.CborHandle{}
Expand Down
2 changes: 1 addition & 1 deletion core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (k DataStoreKey) PrefixEnd() DataStoreKey {
func (k DataStoreKey) FieldID() (uint32, error) {
fieldID, err := strconv.Atoi(k.FieldId)
if err != nil {
return 0, errors.Wrap("Failed to get FieldID of Key", err)
return 0, errors.Wrap("failed to get FieldID of Key", err)
}
return uint32(fieldID), nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/replicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

var (
// ErrMismatchedMergeType - Tying to merge two ReplicatedData of different types
ErrMismatchedMergeType = errors.New("Given type to merge does not match source")
ErrMismatchedMergeType = errors.New("given type to merge does not match source")
)

// ReplicatedData is a data type that allows concurrent writers
Expand Down
2 changes: 1 addition & 1 deletion db/base/collection_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func MakePrimaryIndexKeyForCRDT(
fieldKey := getFieldKey(c, key, fieldName)
return MakeCollectionKey(c).WithInstanceInfo(fieldKey), nil
}
return core.DataStoreKey{}, errors.New("Invalid CRDT type")
return core.DataStoreKey{}, errors.New("invalid CRDT type")
}

func getFieldKey(
Expand Down
24 changes: 12 additions & 12 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
)

var (
ErrDocumentAlreadyExists = errors.New("A document with the given dockey already exists")
ErrUnknownCRDTArgument = errors.New("Invalid CRDT arguments")
ErrDocumentAlreadyExists = errors.New("a document with the given dockey already exists")
ErrUnknownCRDTArgument = errors.New("invalid CRDT arguments")
ErrUnknownCRDT = errors.New("")
)

Expand Down Expand Up @@ -63,29 +63,29 @@ type collection struct {
// NewCollection returns a pointer to a newly instanciated DB Collection
func (db *db) newCollection(desc client.CollectionDescription) (*collection, error) {
if desc.Name == "" {
return nil, errors.New("Collection requires name to not be empty")
return nil, errors.New("collection requires name to not be empty")
}

if len(desc.Schema.Fields) == 0 {
return nil, errors.New("Collection schema has no fields")
return nil, errors.New("collection schema has no fields")
}

docKeyField := desc.Schema.Fields[0]
if docKeyField.Kind != client.FieldKind_DocKey || docKeyField.Name != "_key" {
return nil, errors.New("Collection schema first field must be a DocKey")
return nil, errors.New("collection schema first field must be a DocKey")
}

desc.Schema.FieldIDs = make([]uint32, len(desc.Schema.Fields))
for i, field := range desc.Schema.Fields {
if field.Name == "" {
return nil, errors.New("Collection schema field missing Name")
return nil, errors.New("collection schema field missing Name")
}
if field.Kind == client.FieldKind_None {
return nil, errors.New("Collection schema field missing FieldKind")
return nil, errors.New("collection schema field missing FieldKind")
}
if (field.Kind != client.FieldKind_DocKey && !field.IsObject()) &&
field.Typ == client.NONE_CRDT {
return nil, errors.New("Collection schema field missing CRDT type")
return nil, errors.New("collection schema field missing CRDT type")
}
desc.Schema.FieldIDs[i] = uint32(i)
desc.Schema.Fields[i].ID = client.FieldID(i)
Expand Down Expand Up @@ -122,7 +122,7 @@ func (db *db) CreateCollection(
return nil, err
}
if exists {
return nil, errors.New("Collection already exists")
return nil, errors.New("collection already exists")
}

colSeq, err := db.getSequence(ctx, core.COLLECTION)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (db *db) CreateCollection(
// GetCollection returns an existing collection within the database
func (db *db) GetCollectionByName(ctx context.Context, name string) (client.Collection, error) {
if name == "" {
return nil, errors.New("Collection name can't be empty")
return nil, errors.New("collection name can't be empty")
}

key := core.NewCollectionKey(name)
Expand Down Expand Up @@ -233,7 +233,7 @@ func (db *db) GetCollectionBySchemaID(
schemaID string,
) (client.Collection, error) {
if schemaID == "" {
return nil, errors.New("Schema ID can't be empty")
return nil, errors.New("schema ID can't be empty")
}

key := core.NewCollectionSchemaKey(schemaID)
Expand All @@ -256,7 +256,7 @@ func (db *db) GetAllCollections(ctx context.Context) ([]client.Collection, error
KeysOnly: true,
})
if err != nil {
return nil, errors.Wrap("Failed to create collection prefix query", err)
return nil, errors.Wrap("failed to create collection prefix query", err)
}
defer func() {
if err := q.Close(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions db/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
)

var (
ErrDeleteTargetEmpty = errors.New("The doc delete targeter cannot be empty")
ErrDeleteEmpty = errors.New("The doc delete cannot be empty")
ErrDeleteTargetEmpty = errors.New("the doc delete targeter cannot be empty")
ErrDeleteEmpty = errors.New("the doc delete cannot be empty")
)

// DeleteWith deletes a target document. Target can be a Filter statement,
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *collection) applyFullDelete(
// Get all the heads (cids).
heads, _, err := headset.List(ctx)
if err != nil {
return errors.Wrap("Failed to get document heads", err)
return errors.Wrap("failed to get document heads", err)
}

dagDel := newDagDeleter(txn.DAGstore())
Expand Down
18 changes: 9 additions & 9 deletions db/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestNewCollectionReturnsErrorGivenDuplicateSchema(t *testing.T) {
assert.NoError(t, err)

_, err = newTestCollectionWithSchema(ctx, db)
assert.Errorf(t, err, "Collection already exists")
assert.Errorf(t, err, "collection already exists")
}

func TestNewCollectionReturnsErrorGivenNoFields(t *testing.T) {
Expand All @@ -117,7 +117,7 @@ func TestNewCollectionReturnsErrorGivenNoFields(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection schema has no fields")
assert.EqualError(t, err, "collection schema has no fields")
}

func TestNewCollectionReturnsErrorGivenNoName(t *testing.T) {
Expand All @@ -133,7 +133,7 @@ func TestNewCollectionReturnsErrorGivenNoName(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection requires name to not be empty")
assert.EqualError(t, err, "collection requires name to not be empty")
}

func TestNewCollectionReturnsErrorGivenNoKeyField(t *testing.T) {
Expand All @@ -155,7 +155,7 @@ func TestNewCollectionReturnsErrorGivenNoKeyField(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection schema first field must be a DocKey")
assert.EqualError(t, err, "collection schema first field must be a DocKey")
}

func TestNewCollectionReturnsErrorGivenKeyFieldIsNotFirstField(t *testing.T) {
Expand All @@ -181,7 +181,7 @@ func TestNewCollectionReturnsErrorGivenKeyFieldIsNotFirstField(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection schema first field must be a DocKey")
assert.EqualError(t, err, "collection schema first field must be a DocKey")
}

func TestNewCollectionReturnsErrorGivenFieldWithNoName(t *testing.T) {
Expand All @@ -207,7 +207,7 @@ func TestNewCollectionReturnsErrorGivenFieldWithNoName(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection schema field missing Name")
assert.EqualError(t, err, "collection schema field missing Name")
}

func TestNewCollectionReturnsErrorGivenFieldWithNoKind(t *testing.T) {
Expand All @@ -232,7 +232,7 @@ func TestNewCollectionReturnsErrorGivenFieldWithNoKind(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection schema field missing FieldKind")
assert.EqualError(t, err, "collection schema field missing FieldKind")
}

func TestNewCollectionReturnsErrorGivenFieldWithNoType(t *testing.T) {
Expand All @@ -257,7 +257,7 @@ func TestNewCollectionReturnsErrorGivenFieldWithNoType(t *testing.T) {
}

_, err = db.CreateCollection(ctx, desc)
assert.EqualError(t, err, "Collection schema field missing CRDT type")
assert.EqualError(t, err, "collection schema field missing CRDT type")
}

func TestGetCollectionByName(t *testing.T) {
Expand Down Expand Up @@ -303,5 +303,5 @@ func TestGetCollectionByNameReturnsErrorGivenEmptyString(t *testing.T) {
assert.NoError(t, err)

_, err = db.GetCollectionByName(ctx, "")
assert.EqualError(t, err, "Collection name can't be empty")
assert.EqualError(t, err, "collection name can't be empty")
}
14 changes: 7 additions & 7 deletions db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
)

var (
ErrUpdateTargetEmpty = errors.New("The doc update targeter cannot be empty")
ErrUpdateEmpty = errors.New("The doc update cannot be empty")
ErrUpdateTargetEmpty = errors.New("the doc update targeter cannot be empty")
ErrUpdateEmpty = errors.New("the doc update cannot be empty")
ErrInvalidMergeValueType = errors.New(
"The type of value in the merge patch doesn't match the schema",
"the type of value in the merge patch doesn't match the schema",
)
)

Expand Down Expand Up @@ -604,7 +604,7 @@ func (c *collection) makeSelectionQuery(
switch fval := filter.(type) {
case string:
if fval == "" {
return nil, errors.New("Invalid filter")
return nil, errors.New("invalid filter")
}
var p *parser.Filter
p, err = parser.NewFilterFromString(fval)
Expand All @@ -615,10 +615,10 @@ func (c *collection) makeSelectionQuery(
case *mapper.Filter:
f = fval
default:
return nil, errors.New("Invalid filter")
return nil, errors.New("invalid filter")
}
if filter == "" {
return nil, errors.New("Invalid filter")
return nil, errors.New("invalid filter")
}
slct, err := c.makeSelectLocal(f, mapping)
if err != nil {
Expand Down Expand Up @@ -697,7 +697,7 @@ func (c *collection) getTargetKeyForPatchPath( //nolint:unused
) (string, error) {
_, length := splitPatchPath(path)
if length == 0 {
return "", errors.New("Invalid patch op path")
return "", errors.New("invalid patch op path")
}

return "", nil
Expand Down
8 changes: 4 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ var (
log = logging.MustNewLogger("defra.db")
// ErrDocVerification occurs when a documents contents fail the verification during a Create()
// call against the supplied Document Key.
ErrDocVerification = errors.New("The document verification failed")
ErrDocVerification = errors.New("the document verification failed")

ErrOptionsEmpty = errors.New("Empty options configuration provided")
ErrOptionsEmpty = errors.New("empty options configuration provided")
)

// make sure we match our client interface
Expand Down Expand Up @@ -198,11 +198,11 @@ func (db *db) GetRelationshipIdField(fieldName, targetType, thisType string) (st
rm := db.schema.Relations
rel := rm.GetRelationByDescription(fieldName, targetType, thisType)
if rel == nil {
return "", errors.New("Relation does not exists")
return "", errors.New("relation does not exists")
}
subtypefieldname, _, ok := rel.GetFieldFromSchemaType(targetType)
if !ok {
return "", errors.New("Relation is missing referenced field")
return "", errors.New("relation is missing referenced field")
}
return subtypefieldname, nil
}
Expand Down
Loading

0 comments on commit f96fe72

Please sign in to comment.