Result to Response #4621
Unanswered
wwall3r
asked this question in
Questions & support
Replies: 1 comment 5 replies
-
I would recommend not converting to a response early and keep it all one result type until the end. This removes this issue and also keeps a good separation of concerns between domain logic and the HTTP representation. I would also likely avoid snag in this area as it holds little information that can be used to build HTTP responses. I suspect this info-sparse error type may have been what pushed you towards making the response early. Check out the Wisp guides, they show a recommended pattern for error handling. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I'm working on a small project to try out Gleam in a larger sense (coming from functional in Elixir but have also done a small amount of Rust). I've been working on better error handling. I've adopted
snag
, which helped a lot with not needing boilerplate error types at each level of the codebase.This is a web project, and so most of the things I do (access a database, load a file, hit a remote service) have a
Result
type. Using theuse thing <- result.try(...)
syntax is quite nice as long as your return value is still aResult
. However, at some point, that result has to become aResponse
. Initially I had nestedcase
statements, but of course that degrades quickly if you have more than 2 things you need to do. I liked the notion from Go, where the error is always handled next to the call like so:I ended up making the following helper function, which is similar to
result.try
, but has a function for mapping the error into the expected type:I didn't see anything like that in the stdlib but maybe I'm not on the same page with whatever it is named. In functions which produce responses, that lets me use it like so:
Is there a way of doing this in the stdlib which I am missing? Or am I way off and another pattern would be better?
Beta Was this translation helpful? Give feedback.
All reactions