-
Notifications
You must be signed in to change notification settings - Fork 3
[RFC] Error callback and no more return { data: X }
#17
Conversation
I believe this change is indeed better than the first (error/data) version - less verbose and more familiar for both people who are regular to throw errors, as well for people who are regular to functional error handling (like Either type).
Did you consider using an Either type, or even just importing an existing tree shakeable one like fp-ts's Either? pros:
cons:
|
I'm pretty convinced on it too!
With fp-ts' approach one could return a https://github.com/gcanti/fp-ts/blob/6d6f7d8b725619db509199742b1079d9e7922dc5/src/Either.ts#L63-L85 That said, maybe the Symbol is complicating things unnecessarily? It is a bit of an extreme edge case to cover.
Both solutions have similar underlying mechanisms.
This is interesting.
This part of the code is like a grain of sand in a desert. I am pretty confident I'll be able to test this appropiately.
Yeah, I don't want this. I'd rather letting the compiler automatically add
I think it's solvable too. |
I'm not 100% sure this is true, because Either<R,E> gets two type variables, which may used to enforce the return types of both branches. I will test that when have time.
Just to clarify, you already use a "left" like helper (the trpc.error() utility), the only difference in Either is that you will also have to use something like "trpc.success()".
|
If I were building an fp-ts tutorial site and I had a query
I do think that might be a bit of an adoption concern. I wonder if there's a good way to canvas opinion on this (although things like twitter polls might just find trpc acolytes who are more happy than the average potential user who'd be more likely to be put off by needing to put
|
164563f
to
c2122e6
Compare
Not really anyone asking for it. It started as a challenge to myself but I can see it being quite useful in some cases.... although still not sure it'll make it in the release. Will spin-off that discussion to #28 and merge this one for now |
Instead of having all procedures to return a
{ data: X } | { error: Y }
shape, use anerror({ code: 'X ', /* additional data */})
Questions
error()
- maybe aninvalid()
/bad()
-helper would be more suitableError
, it's more about signalling a bad request - proper thrown errors will be handled in a similar fashion of what they are today.Benefits
undefined
, which is common for mutations, you don't have toreturn
anything in the fnExample use
The result of the above is:
Details
Symbol
that is easily identifiable both in code and in genericsSuggestion by @mmkal