diff --git a/internal/client/client.go b/internal/client/client.go index 5ca4740..11332df 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -73,7 +73,7 @@ func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, err } if !exists { - return nil, errors.New("key does not exist") + return nil, types.ErrKeyNotFound } err = c.db.CreateShare(ctx, share) diff --git a/internal/db/mock.go b/internal/db/mock.go index 7a24b20..46bfd54 100644 --- a/internal/db/mock.go +++ b/internal/db/mock.go @@ -31,7 +31,7 @@ func (d *mockDB) CreateShare(ctx context.Context, share *types.Share) error { func (d *mockDB) DeleteShare(ctx context.Context, slug string) error { if d.shares[slug] == nil { - return errors.New("share does not exist") + return types.ErrShareNotFound } delete(d.shares, slug) return nil diff --git a/internal/db/sql.go b/internal/db/sql.go index a38e31b..ad31270 100644 --- a/internal/db/sql.go +++ b/internal/db/sql.go @@ -79,10 +79,20 @@ func (db *sqlDB) CreateShare(ctx context.Context, share *types.Share) error { } func (db *sqlDB) DeleteShare(ctx context.Context, slug string) error { - _, err := db.db.ExecContext(ctx, "DELETE FROM shares WHERE slug = ?", slug) + result, err := db.db.ExecContext(ctx, "DELETE FROM shares WHERE slug = ?", slug) if err != nil { return err } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + return types.ErrShareNotFound + } + return nil } diff --git a/internal/types/errors.go b/internal/types/errors.go new file mode 100644 index 0000000..8c9301b --- /dev/null +++ b/internal/types/errors.go @@ -0,0 +1,6 @@ +package types + +import "errors" + +var ErrKeyNotFound = errors.New("Key not found") +var ErrShareNotFound = errors.New("Share not found") diff --git a/internal/web/web.go b/internal/web/web.go index f2da787..c5ff90b 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -117,6 +117,10 @@ func CreateRouter(client *client.Client, username, password string) *mux.Router share, err := client.CreateShare(r.Context(), shareParams.Key) if err != nil { + if err == types.ErrKeyNotFound { + http.Error(w, "The specified key does not exist", http.StatusBadRequest) + return + } logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -136,6 +140,10 @@ func CreateRouter(client *client.Client, username, password string) *mux.Router err := client.DeleteShare(r.Context(), vars["slug"]) if err != nil { + if err == types.ErrShareNotFound { + http.NotFound(w, r) + return + } logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return