From fad10f31044426209a48346c54b45fa894e6b14e Mon Sep 17 00:00:00 2001 From: Eder Manoel Cale Costa Date: Sun, 15 Sep 2019 23:12:06 -0300 Subject: [PATCH] Create a pattern for error --- .gitignore | 2 ++ example/fishing.go | 2 +- scribble.go | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 8d3a228..9be4771 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ _testmain.go # .DS_Store TODO + +.idea/ \ No newline at end of file diff --git a/example/fishing.go b/example/fishing.go index 302df2d..db734e4 100644 --- a/example/fishing.go +++ b/example/fishing.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" - scribble "github.com/nanobox-io/golang-scribble" + scribble "github.com/edermanoel94/golang-scribble" ) // a fish diff --git a/scribble.go b/scribble.go index 65844ce..a46a371 100644 --- a/scribble.go +++ b/scribble.go @@ -3,6 +3,7 @@ package scribble import ( "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -15,6 +16,11 @@ import ( // Version is the current version of the project const Version = "1.0.4" +var ( + ErrMissingResource = errors.New("missing resource - unable to save record, no name") + ErrMissingCollection = errors.New("missing collection - no place to save record") +) + type ( // Logger is a generic logger interface @@ -86,12 +92,12 @@ func (d *Driver) Write(collection, resource string, v interface{}) error { // ensure there is a place to save record if collection == "" { - return fmt.Errorf("Missing collection - no place to save record!") + return ErrMissingCollection } // ensure there is a resource (name) to save record as if resource == "" { - return fmt.Errorf("Missing resource - unable to save record (no name)!") + return ErrMissingResource } mutex := d.getOrCreateMutex(collection) @@ -128,12 +134,12 @@ func (d *Driver) Read(collection, resource string, v interface{}) error { // ensure there is a place to save record if collection == "" { - return fmt.Errorf("Missing collection - no place to save record!") + return ErrMissingCollection } // ensure there is a resource (name) to save record as if resource == "" { - return fmt.Errorf("Missing resource - unable to save record (no name)!") + return ErrMissingResource } // @@ -160,7 +166,7 @@ func (d *Driver) ReadAll(collection string) ([]string, error) { // ensure there is a collection to read if collection == "" { - return nil, fmt.Errorf("Missing collection - unable to record location!") + return nil, ErrMissingCollection } // @@ -236,7 +242,7 @@ func stat(path string) (fi os.FileInfo, err error) { } // getOrCreateMutex creates a new collection specific mutex any time a collection -// is being modfied to avoid unsafe operations +// is being modified to avoid unsafe operations func (d *Driver) getOrCreateMutex(collection string) *sync.Mutex { d.mutex.Lock()