Closed cideM closed 3 years ago
Ah this is an interesting observation! I think you could probably implement a similar behavior using a custom type that you could throw on, e.g.:
data MaybeInt = MiInt Int | MiOther
instance FromHttpApiData MaybeInt of
parseUrlPiece x =
case parseUrlPiece x of
Left _ -> Other
Right i -> MiInt i
get ("hello" <//> var) $ \(name :: MaybeInt) ->
case name of
MiInt i -> ok i
MiOther -> throw ...
But I'm happy to add this natively to the library too. One option is to introduce an alternative combinator to var
. The other option would be to make this globally configurable. I think the alternative combinator is probably the best route?
I think that does indeed sound very nice
👍 Cool feel free to send a PR!
This is something that many Haskell route libraries are plagued with, I think.
I modified the snippet from the hello world project, so that it requires an
Int
:http://localhost:8080/hello/foo
-> 404http://localhost:8080/hello/2
-> 200I think that there's no universally correct answer for what should happen in the
/hello/foo
case. But I'd like to have the option of returning an error message to my users. In this case it's likely that someone wants to access thehello/*
route, but didn't remember the accepted parameters. I would like to return a 400 Bad Request in this case.Right now the only way I can imagine this would work is to always use
Text
and then manually cast types. But then it's pretty much like WAI pattern matching in the style of["hello", var] -> case readEither var of
.It would be nice if that behavior was at least documented :book: would you accept a PR for that?
~On a somewhat related note, most type safe route libs in Haskell seem to be similar in what they achieve. What would be really cool was if route matching would be checked for totality. In this case the compiler should then warn that I'm missing patterns for
var
.~I think what would work quite well would be an exception that you can catch like so: