总的来说,我认为这不值得做,因为
一般来说
,可能会有非常复杂的路由组合,产生非常不直观的错误消息。例如,考虑下面的API:
type API =
"position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
:<|> "position" :> "foo" :> "test" :> Get '[JSON] Position
这个API允许
/position/1/2
和
/position/foo/test
,但拒绝
/position/1/test
,并且对于最后一个案例,您无法生成任何正常的错误消息。它必须是类似于“
期望在最末端有一个Int,或者在第二个from end位置有一个“foo”,但在末端有一个“test”,在第二个from end位置有一个“1”
“。对消费者没有帮助。
“如果您想要创建一条经过适当格式化的路径,则只需创建一条经过适当格式化的路径即可:
type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
:<|> "position" :> Capture "x" Text :> Capture "y" Text :> Get '[JSON] ()
:<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
:<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
...
server3 = position
:<|> badPosition
:<|> hello
:<|> marketing
where
...
badPosition x y =
throwError $ err400 { errBody = "Expected ints, got '" <> x <> "' and '" <> y <> "'" }