Create a pattern for error
This commit is contained in:
parent
aa3e7c1189
commit
fad10f3104
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -26,3 +26,5 @@ _testmain.go
|
||||||
#
|
#
|
||||||
.DS_Store
|
.DS_Store
|
||||||
TODO
|
TODO
|
||||||
|
|
||||||
|
.idea/
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
scribble "github.com/nanobox-io/golang-scribble"
|
scribble "github.com/edermanoel94/golang-scribble"
|
||||||
)
|
)
|
||||||
|
|
||||||
// a fish
|
// a fish
|
||||||
|
|
18
scribble.go
18
scribble.go
|
@ -3,6 +3,7 @@ package scribble
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -15,6 +16,11 @@ import (
|
||||||
// Version is the current version of the project
|
// Version is the current version of the project
|
||||||
const Version = "1.0.4"
|
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 (
|
type (
|
||||||
|
|
||||||
// Logger is a generic logger interface
|
// 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
|
// ensure there is a place to save record
|
||||||
if collection == "" {
|
if collection == "" {
|
||||||
return fmt.Errorf("Missing collection - no place to save record!")
|
return ErrMissingCollection
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure there is a resource (name) to save record as
|
// ensure there is a resource (name) to save record as
|
||||||
if resource == "" {
|
if resource == "" {
|
||||||
return fmt.Errorf("Missing resource - unable to save record (no name)!")
|
return ErrMissingResource
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex := d.getOrCreateMutex(collection)
|
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
|
// ensure there is a place to save record
|
||||||
if collection == "" {
|
if collection == "" {
|
||||||
return fmt.Errorf("Missing collection - no place to save record!")
|
return ErrMissingCollection
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure there is a resource (name) to save record as
|
// ensure there is a resource (name) to save record as
|
||||||
if resource == "" {
|
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
|
// ensure there is a collection to read
|
||||||
if collection == "" {
|
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
|
// 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 {
|
func (d *Driver) getOrCreateMutex(collection string) *sync.Mutex {
|
||||||
|
|
||||||
d.mutex.Lock()
|
d.mutex.Lock()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user