Skip to content

Commit

Permalink
add error handler for unreachable destination
Browse files Browse the repository at this point in the history
  • Loading branch information
Vector-Hector committed Dec 15, 2023
1 parent 60e20da commit 5a49706
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (b *Bifrost) RouteOnlyTimeIndependent(rounds *Rounds, origins []SourceKey,

_, ok := rounds.EarliestArrivals[destKey]
if !ok {
panic("destination unreachable")
panic(NoRouteError(true))
}

journey := b.ReconstructJourney(destKey, 1, rounds)
Expand Down
9 changes: 9 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bifrost

import "fmt"

type NoRouteError bool

func (e NoRouteError) Error() string {
return fmt.Sprintf("no route found")
}
2 changes: 1 addition & 1 deletion raptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (b *Bifrost) RouteTransit(rounds *Rounds, origins []SourceKey, destKey uint

_, ok = rounds.EarliestArrivals[destKey]
if !ok {
return nil, fmt.Errorf("destination unreachable")
return nil, NoRouteError(true)
}

journey := b.ReconstructJourney(destKey, lastRound, rounds)
Expand Down
19 changes: 15 additions & 4 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,24 @@ func main() {

func handle(c *gin.Context, b *bifrost.Bifrost) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
r := recover()

debug.PrintStack()
if r == nil {
return
}

c.String(500, "Internal server error: %v", r)
if _, ok := r.(bifrost.NoRouteError); ok {
c.JSON(404, gin.H{
"error": "no route found",
})
return
}

fmt.Println("Recovered in f", r)

debug.PrintStack()

c.String(500, "Internal server error: %v", r)
}()

req := &JourneyRequest{}
Expand Down

0 comments on commit 5a49706

Please sign in to comment.