Skip to content

Commit

Permalink
feat: Include dockey in doc exists err (#1558)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #1557

## Description

Includes the dockey in the doc exists err, and the doc deleted error.
  • Loading branch information
AndrewSisley authored Jun 2, 2023
1 parent 8055b8c commit a6224af
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,10 +653,10 @@ func (c *collection) create(ctx context.Context, txn datastore.Txn, doc *client.
return err
}
if exists {
return ErrDocumentAlreadyExists
return NewErrDocumentAlreadyExists(primaryKey.DocKey)
}
if isDeleted {
return ErrDocumentDeleted
return NewErrDocumentDeleted(primaryKey.DocKey)
}

// write value object marker if we have an empty doc
Expand Down Expand Up @@ -696,7 +696,7 @@ func (c *collection) Update(ctx context.Context, doc *client.Document) error {
return client.ErrDocumentNotFound
}
if isDeleted {
return ErrDocumentDeleted
return NewErrDocumentDeleted(primaryKey.DocKey)
}

err = c.update(ctx, txn, doc)
Expand Down Expand Up @@ -886,7 +886,7 @@ func (c *collection) Delete(ctx context.Context, key client.DocKey) (bool, error
return false, client.ErrDocumentNotFound
}
if isDeleted {
return false, ErrDocumentDeleted
return false, NewErrDocumentDeleted(primaryKey.DocKey)
}

err = c.applyDelete(ctx, txn, primaryKey)
Expand Down
2 changes: 1 addition & 1 deletion db/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (c *collection) applyDelete(
return client.ErrDocumentNotFound
}
if isDeleted {
return ErrDocumentDeleted
return NewErrDocumentDeleted(key.DocKey)
}

dsKey := key.ToDataStoreKey()
Expand Down
20 changes: 18 additions & 2 deletions db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
errInvalidCRDTType string = "only default or LWW (last writer wins) CRDT types are supported"
errCannotDeleteField string = "deleting an existing field is not supported"
errFieldKindNotFound string = "no type found for given name"
errDocumentAlreadyExists string = "a document with the given dockey already exists"
errDocumentDeleted string = "a document with the given dockey has been deleted"
)

var (
Expand All @@ -57,8 +59,8 @@ var (
ErrMergeSubTypeNotSupported = errors.New("merge doesn't support sub types yet")
ErrInvalidFilter = errors.New("invalid filter")
ErrInvalidOpPath = errors.New("invalid patch op path")
ErrDocumentAlreadyExists = errors.New("a document with the given dockey already exists")
ErrDocumentDeleted = errors.New("a document with the given dockey has been deleted")
ErrDocumentAlreadyExists = errors.New(errDocumentAlreadyExists)
ErrDocumentDeleted = errors.New(errDocumentDeleted)
ErrUnknownCRDTArgument = errors.New("invalid CRDT arguments")
ErrUnknownCRDT = errors.New("unknown crdt")
ErrSchemaFirstFieldDocKey = errors.New("collection schema first field must be a DocKey")
Expand Down Expand Up @@ -214,3 +216,17 @@ func NewErrCannotDeleteField(name string, id client.FieldID) error {
errors.NewKV("ID", id),
)
}

func NewErrDocumentAlreadyExists(dockey string) error {
return errors.New(
errDocumentAlreadyExists,
errors.NewKV("DocKey", dockey),
)
}

func NewErrDocumentDeleted(dockey string) error {
return errors.New(
errDocumentDeleted,
errors.NewKV("DocKey", dockey),
)
}
2 changes: 1 addition & 1 deletion tests/integration/mutation/simple/create/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestMutationCreateSimpleDoesNotCreateDocGivenDuplicate(t *testing.T) {
}`,
},
},
ExpectedError: "a document with the given dockey already exists",
ExpectedError: "a document with the given dockey already exists. DocKey: ",
}

simpleTests.ExecuteTestCase(t, test)
Expand Down

0 comments on commit a6224af

Please sign in to comment.