Skip to content

Commit

Permalink
database: make ErrSameAlias public, improve tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgooz committed Nov 23, 2018
1 parent 8bd44fe commit 4877df0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions database/service_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (d *LevelDBServiceDB) Save(s *service.Service) error {
return err
}
} else {
return &errSameAlias{alias: s.Alias}
return &ErrSameAlias{alias: s.Alias}
}

if err := d.aliases.Put([]byte(s.Alias), []byte(s.ID), nil); err != nil {
Expand Down Expand Up @@ -191,16 +191,17 @@ func (e *DecodeError) Error() string {
return fmt.Sprintf("Database services: Could not decode service %q", e.ID)
}

// IsErrNotFound returs true if err is type of ErrNotFound, false otherwise.
// IsErrNotFound returns true if err is type of ErrNotFound, false otherwise.
func IsErrNotFound(err error) bool {
_, ok := err.(*ErrNotFound)
return ok
}

type errSameAlias struct {
// ErrSameAlias error returned when there is a service with the same alias.
type ErrSameAlias struct {
alias string
}

func (e *errSameAlias) Error() string {
func (e *ErrSameAlias) Error() string {
return fmt.Sprintf("database: a service with the %q alias already exists", e.alias)
}
2 changes: 1 addition & 1 deletion database/service_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestServiceDBSaveWithAlias(t *testing.T) {

// try saving a service with the same alias.
s = &service.Service{ID: "3", Alias: "2", Name: "test-service"}
require.Error(t, db.Save(s))
require.Equal(t, &ErrSameAlias{alias: "2"}, db.Save(s))
}

func TestServiceDBGet(t *testing.T) {
Expand Down

0 comments on commit 4877df0

Please sign in to comment.