commit cce63097ccdb2bae0c9945eb4132f9837a69dd1c
parent 335c8a5e8bae5fed3d218c3d5ec11b339168e87f
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 2 Oct 2022 12:49:46 -0400
better error for input validation
Diffstat:
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/internal/backend/actions.go b/internal/backend/actions.go
@@ -3,6 +3,7 @@ package backend
import (
"errors"
+ "fmt"
"os"
"path/filepath"
"strings"
@@ -154,7 +155,7 @@ func splitComponents(path string) ([]string, string, error) {
title := filepath.Base(path)
parts := strings.Split(filepath.Dir(path), string(os.PathSeparator))
if len(parts) < 2 {
- return nil, "", errors.New("invalid component path")
+ return nil, "", fmt.Errorf("input paths must contain at LEAST 3 components (e.g. abc%c123%cxyz)", os.PathSeparator, os.PathSeparator)
}
return parts, title, nil
}
diff --git a/internal/backend/actions_test.go b/internal/backend/actions_test.go
@@ -74,10 +74,10 @@ func TestInserts(t *testing.T) {
if err := setup(t).Insert("", ""); err.Error() != "empty path not allowed" {
t.Errorf("wrong error: %v", err)
}
- if err := setup(t).Insert(filepath.Join("test", "offset"), "test"); err.Error() != "invalid component path" {
+ if err := setup(t).Insert(filepath.Join("test", "offset"), "test"); err.Error() != "input paths must contain at LEAST 3 components (e.g. abc/123/xyz)" {
t.Errorf("wrong error: %v", err)
}
- if err := setup(t).Insert("test", "test"); err.Error() != "invalid component path" {
+ if err := setup(t).Insert("test", "test"); err.Error() != "input paths must contain at LEAST 3 components (e.g. abc/123/xyz)" {
t.Errorf("wrong error: %v", err)
}
if err := setup(t).Insert("a", ""); err.Error() != "empty secret not allowed" {
@@ -115,7 +115,7 @@ func TestRemoves(t *testing.T) {
if err := setup(t).Remove(nil); err.Error() != "entity is empty/invalid" {
t.Errorf("wrong error: %v", err)
}
- if err := setup(t).Remove(&backend.QueryEntity{}); err.Error() != "invalid component path" {
+ if err := setup(t).Remove(&backend.QueryEntity{}); err.Error() != "input paths must contain at LEAST 3 components (e.g. abc/123/xyz)" {
t.Errorf("wrong error: %v", err)
}
if err := setup(t).Remove(&backend.QueryEntity{Path: filepath.Join("test1", "test2", "test3")}); err.Error() != "failed to remove entity" {