Skip to content

What to do with web svc Router endpoint defintion fallibility #757

@GlenDC

Description

@GlenDC

In

#[allow(clippy::expect_used, reason = "TODO later")]
self.routes
.insert(path, vec![(matcher, service)])
.expect("add route");
we currently panic in case we fail insertin an endpoint svc into the router. As far as I know this can fail in two cases:

  1. duplicate insertion (for the path)
  2. invalid path

AFAIK the duplication should not be happening as we check if it exists and if so push if it does and only if it doesn't we do insert. Not perfect, but at least that should normally prevent that. The invalid path however could still happen.

Some frameworks like Axum panic for this and many other reasons as they assume that's fine given it will happen when you start the app and so immediately caught. However:

  1. what if you create the router in a spawned thread? Than that thread will fail silentely (unless you join) making your process with one (router) server less
  2. what if you create the router not at startup time but dynamically at runtime during any point of the lifetime other than startup?

In all these cases it's a bit crazy to panic... In my opinion panics should only be reserved for very bad stuff like OOM or loco invalid unrecoverable nuclear disaster states. Certainly not for oops I failed to create a router dynamically at runtime...

Even with this context given that doesn't make the solution easier... What to do with this?

  • should we just make all methods fallible (e.g. try_get instead of get as all of them can fail?
  • should we instead make it a builder like Request::builder and internally work with a Result so that you only have to deal with the error once? More ergonomic but perhaps a bit weird as you can't really act on that kind of error and it's like a fail all or nothing force move...
  • work with a more awkward API, perhaps even having to fork the underlying lib so that we can prevent this all together somehow? Or perhaps force people to give verified paths that cannot fail, fine when at runtime, and for static definitions we could provide macro?
  • ignore failed inserts? Probably not... seems like a very bad thing to do

None of these options are ideal and perhaps the is another option... We need to fix this before we ship 0.3 as it will be a big breaking change of the router API regardless of the option we choose.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions