Returning monads/Result
types in controllers
#4843
-
I'm using the export type Result<T, E = Error> =
| { ok: true; value: T }
| { ok: false; error: E }; Ideally I would like to be able to return such values from within my controllers. The strength of this paradigm means that I have very explicit success/failure values, and I can properly handle them everywhere, when it makes sense. Now, I would like these types to be the ones I return from my controller's functions. This allows my controller's functions to return each time either a success or failure value. If I do it right now, it's completely useless. 1. My Is there a mechanism that would allow me to centralize the handling of these types? I can see that there is a centralized exception handling, however... This only applies to controllers that Basically, I'm looking for an "outgoing" middleware in AdonisJS... I've read the documentation and guides, but couldn't find anything that matches what I'm describing. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Note that one solution would be to systematically call the same function at the end of every single controller, something that |
Beta Was this translation helpful? Give feedback.
-
I think middleware should be enough to do what you want. Check this : https://docs.adonisjs.com/guides/basics/middleware#middleware-execution-flow Basically, anything called after an |
Beta Was this translation helpful? Give feedback.
I think middleware should be enough to do what you want. Check this : https://docs.adonisjs.com/guides/basics/middleware#middleware-execution-flow
Basically, anything called after an
await next
allows you to have theoutgoing middleware
you were describing