Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Use a custom type for RecordNotFound errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Apr 25, 2018
1 parent 35e9493 commit d44d68c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 9 deletions.
20 changes: 18 additions & 2 deletions db/database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package db

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -23,6 +23,21 @@ type DynamodbDatabase struct {
Table string
}

// RecordNotFound is an error type returned when no record was returnd from the database
type RecordNotFound struct {
ID *string
Version *string
}

// Error returns the string representation of the error.
// satisfying the error interface
func (e RecordNotFound) Error() string {
if e.Version != nil {
return fmt.Sprintf("Unable to find record for %s with version: %s", *e.ID, *e.Version)
}
return fmt.Sprintf("Unable to find record for %s", *e.ID)
}

// Connect creates a dynamodb connection
func Connect(session *session.Session, dynamodbEndpoint string) *dynamodb.DynamoDB {
dynamoConfig := &aws.Config{Endpoint: aws.String(dynamodbEndpoint)}
Expand All @@ -35,8 +50,9 @@ func (d *DynamodbDatabase) query(params *dynamodb.QueryInput) (*datautils.Resour
if err != nil {
return nil, err
}

if len(resp.Items) == 0 {
return nil, errors.New("not found")
return nil, &RecordNotFound{}
}

return respToResource(resp.Items[0])
Expand Down
11 changes: 10 additions & 1 deletion db/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ func TestRetrieveLatestNotFound(t *testing.T) {
t.Skip("skipping integration test in short mode")
}
_, err := initDatabase().RetrieveLatest("8888")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Unable to find record for 8888")
}

func TestRetrieveVersionNotFound(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
version := "9"
_, err := initDatabase().RetrieveVersion("8888", &version)
assert.Equal(t, err.Error(), "Unable to find record for 8888 with version: 9")
}

func initDatabase() Database {
Expand Down
2 changes: 1 addition & 1 deletion db/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (h *DynamodbDatabase) DeleteAllVersions(externalID string) error {
panic(err)
}
resource, err = h.RetrieveLatest(externalID)
if err != nil && err.Error() != "not found" {
if _, ok := err.(*RecordNotFound); !ok {
panic(err)
}
}
Expand Down
6 changes: 5 additions & 1 deletion db/retrieve_latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ func (d *DynamodbDatabase) RetrieveLatest(externalID string) (*datautils.Resourc
TableName: &d.Table,
}

return d.query(params)
result, err := d.query(params)
if tmp, ok := err.(*RecordNotFound); ok {
tmp.ID = &externalID
}
return result, err
}
7 changes: 6 additions & 1 deletion db/retrieve_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ func (d *DynamodbDatabase) RetrieveVersion(externalID string, version *string) (
TableName: &d.Table,
}

return d.query(params)
result, err := d.query(params)
if tmp, ok := err.(*RecordNotFound); ok {
tmp.ID = &externalID
tmp.Version = version
}
return result, err
}
2 changes: 1 addition & 1 deletion handlers/retrieve_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (d *retrieveResource) Handle(params operations.RetrieveResourceParams, agen
}

if err != nil {
if err.Error() == "not found" {
if _, ok := err.(*db.RecordNotFound); ok {
return operations.NewRetrieveResourceNotFound()
}
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion handlers/stub_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (d *MockDatabase) RetrieveLatest(externalID string) (*datautils.Resource, e
d.record = nil
return record, nil
}
return nil, errors.New("not found")
return nil, &db.RecordNotFound{ID: &externalID}
}

func (d *MockDatabase) RetrieveVersion(externalID string, version *string) (*datautils.Resource, error) {
Expand Down
2 changes: 1 addition & 1 deletion handlers/update_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (d *updateResourceEntry) Handle(params operations.UpdateResourceParams) mid

existingResource, err := d.database.RetrieveLatest(id)
if err != nil {
if err.Error() == "not found" {
if _, ok := err.(*db.RecordNotFound); ok {
return operations.NewUpdateResourceNotFound()
}
panic(err)
Expand Down

0 comments on commit d44d68c

Please sign in to comment.