Skip to content

Commit

Permalink
add gates.NotFound and gates.MultipleCandidates errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Sep 30, 2021
1 parent 0373b45 commit 58ed3e1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Go package for working with architectural elements, in a SFO Museum context.

## Documentation

[![Go Reference](https://pkg.go.dev/badge/github.com/sfomuseum/go-sfomuseum-architecture.svg)](https://pkg.go.dev/github.com/sfomuseum/go-sfomuseum-architecture)

Documentation is incomplete at this time.

## Important
Expand Down
2 changes: 1 addition & 1 deletion data/galleries.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/gates.json

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions gates/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gates

import (
"fmt"
)

type NotFound struct{ code string }

func (e NotFound) Error() string {
return fmt.Sprintf("Gate '%s' not found", e.code)
}

func (e NotFound) String() string {
return e.Error()
}

type MultipleCandidates struct{ code string }

func (e MultipleCandidates) Error() string {
return fmt.Sprintf("Multiple candidates for gate '%s'", e.code)
}

func (e MultipleCandidates) String() string {
return e.Error()
}

func IsNotFound(e error) bool {

switch e.(type) {
case NotFound, *NotFound:
return true
default:
return false
}
}

func IsMultipleCandidates(e error) bool {

switch e.(type) {
case MultipleCandidates, *MultipleCandidates:
return true
default:
return false
}
}
32 changes: 32 additions & 0 deletions gates/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gates

import (
_ "fmt"
"testing"
)

func TestNotFound(t *testing.T) {

e := NotFound{"A6"}

if !IsNotFound(e) {
t.Fatalf("Expected NotFound error")
}

if e.String() != "Gate 'A6' not found" {
t.Fatalf("Invalid stringification")
}
}

func TestMultipleCandidates(t *testing.T) {

e := MultipleCandidates{"A6"}

if !IsMultipleCandidates(e) {
t.Fatalf("Expected MultipleCandidates error")
}

if e.String() != "Multiple candidates for gate 'A6'" {
t.Fatalf("Invalid stringification")
}
}
4 changes: 2 additions & 2 deletions gates/gates.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func FindCurrentGateWithLookup(ctx context.Context, lookup architecture.Lookup,

switch len(current) {
case 0:
return nil, fmt.Errorf("No matches for %s", code)
return nil, NotFound{code}
case 1:
return current[0], nil
default:
return nil, fmt.Errorf("Multiple matches for %s (%v)", code, current)
return nil, MultipleCandidates{code}
}

}
Expand Down

0 comments on commit 58ed3e1

Please sign in to comment.