Repository navigation
Open
Description
This project looks great! I've been doing something similar to interop between Effect and tRPC. The main thing blocking adoption is that it's not clear if there's a real way to handle anything other than successful queries / mutations here: from the source, looks like they'd just throw a FiberError. My approach so far has been to have a known set of tagged errors, like NotFoundError, and to handle them in the adapter:
function mapErrorToTRPC(error: MappableErrors) {
if (error instanceof UnexpectedError) {
Sentry.captureException(error);
}
if (error instanceof RedirectError) {
// This should never happen, tRPC does not support redirects.
return new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: error });
}
if (error instanceof MethodNotAllowedError) {
// TRPC uses an non-standard phrase for this error
return new TRPCError({ code: "METHOD_NOT_SUPPORTED", cause: error });
}
return new TRPCError({ code: error.code, cause: error });
}
/**
* Effect / tRPC crosswalk: this is the same as unwrapForTrpc, but for
* Effect. Automatically provides the database context via shared runtime.
*/
export async function effectTrpc<A, E>(
effect: Effect.Effect<A, E, DbRequirements>
) {
// Explicitly propagate OTel span context to Effect to fix broken trace parenting
const activeSpan = trace.getSpan(context.active());
const effectWithContext = activeSpan
? Tracer.withSpanContext(effect, activeSpan.spanContext())
: effect;
const exit = await runtime.runPromiseExit(effectWithContext);
return Exit.match(exit, {
onFailure(exit) {
return Option.match(Cause.failureOption(exit), {
onNone() {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Unexpected error type",
});
},
onSome(f) {
throw mapErrorToTRPC(f as MappableErrors);
},
});
},
onSuccess(v) {
return v;
},
});
}Is something like this possible with effect-trpc? Generally I think applications would have at least queries with not found states.
Metadata
Metadata
Assignees
Labels
No labels
Activity