DiazCarrete,
@DiazCarrete@hachyderm.io avatar

In Servant, the ServerError type has an Exception instance
https://hackage.haskell.org/package/servant-server-0.20/docs/Servant-Server.html#t:ServerError
You might speculate that when throwing a ServerError using liftIO . throwIO in a Handler, the ServerError is automatically caught and served as a response, but it ain't so: it's treated as just another exception, and the response code is 500.

Instead, you should throw ServerErrors using "throwError", re-exported from the "Servant" module.
https://hackage.haskell.org/package/servant-server-0.20/docs/Servant.html#v:throwError

image/png

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

btw, why am I throwing a ServerError with HTTP code 200?

I think ServerError is somewhat misnamed: in reality, it can represent any type of response, not only errors.

It's a kind of escape hatch from the typing strictures of Servant: it lets you return any status code and respose body for a request, not just the ones specified in the type-level API.

BoydStephenSmithJr,
@BoydStephenSmithJr@hachyderm.io avatar

@DiazCarrete I've never really liked using exception throwing as control flow, but Yesod does something similar for all non-200 and can be used to early-exit with a 200. And, since it's there I have (ab)used it.

DiazCarrete, (edited )
@DiazCarrete@hachyderm.io avatar

@BoydStephenSmithJr I don't think it's a bad idea!

I believe we can impement in Servant the throwIO behavior I mentioned earlier by catching (some) runtime exceptions in the callback passed to "hoistServer" and re-throwing them in the "proper" way expected by Handler. https://hackage.haskell.org/package/servant-server-0.20/docs/Servant-Server.html#v:hoistServer

BoydStephenSmithJr,
@BoydStephenSmithJr@hachyderm.io avatar

@DiazCarrete I think that universally quantified x might be a problem, but I suppose it depends on the type of your re-throw mechanism.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • haskell
  • kavyap
  • InstantRegret
  • ethstaker
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • GTA5RPClips
  • slotface
  • Durango
  • khanakhh
  • rosin
  • everett
  • provamag3
  • vwfavf
  • tacticalgear
  • osvaldo12
  • cisconetworking
  • cubers
  • modclub
  • ngwrru68w68
  • Leos
  • anitta
  • normalnudes
  • tester
  • megavids
  • JUstTest
  • All magazines