Effect CommunityEC
Effect Community11mo ago
9 replies
TldrOlli

Pass existing Context/Layer to Effect.runSync

Hi all,
I'm quite new to effect-ts and just stumbled across some oddity I do not fully understand.

I have a ConnectionManager that executes API requests with axios, where I provide the axios instance as a service.
To execute the request promises, I have the following code:

const res = yield* Effect.tryPromise({
  try: (abortSignal) => instance.request<OpenApiDefinition>({ ...request, signal: abortSignal }),
  catch: (err) => Effect.runSync(this.handleCalloutErrorEff(host, err))
}).pipe(Effect.withSpan('acaad:sync:query:api:request-wait'));
const openApi = yield* ConnectionManager.verifyResponsePayload(res).pipe(
  Effect.withSpan('acaad:sync:query:api:request-parse')
);


The spans above are pushed as expected, showing up in tempo.
However, the spans created in the following (handleCalloutErrorEff) snippet are not published:

private handleCalloutErrorEff(host: AcaadHost, error: unknown) {
  return Effect.gen(function* () {
    let resultError: AcaadError;

    if (error instanceof AxiosError) {
      const axiosError: AxiosError = error;

      if (axiosError.code === 'ECONNREFUSED') {
        resultError = new AcaadServerUnreachableError(host, axiosError);
      }

      if (axiosError.code === 'ERR_BAD_REQUEST' || axiosError.code === 'ERR_BAD_RESPONSE') {
        resultError = new ResponseStatusCodeError(host, 200, axiosError.status, axiosError);
      }
    }

    resultError ??= new CalloutError(error);

    yield* Effect.annotateCurrentSpan('raised-error', resultError);

    return resultError;
  }).pipe(Effect.withSpan('acaad:conn:error-mapper'));
}


My best guess is that the context/layer of opentel is not shared the moment I call Effect.runSync and the contexts are completely different.
Is this the correct assumption? If yes, how could I provide the same (?) context to the error handling effect?
Is the approach wrong in general?

Any advice would be highly appreciated, thanks in advance!
Was this page helpful?