Effect CommunityEC
Effect Community•3y ago•
7 replies
Sanadriu.dev

Troubleshooting exceptions and unwrapping with Effect.mapError or Effect.tapError

Hi. I am having problems with exceptions, as they are not unwrapped with Effect.mapError or Effect.tapError.

{
  "errorType": "TypeError",
  "errorMessage": "r.getDetails is not a function",
  "trace": [
    "TypeError: r.getDetails is not a function",
    "    at failure (/src/core/layers/application/_common/middlewares/ErrorLogger.middleware.ts:14:51)",
     ...
  ]
}


The issue is that, instead of giving me the failure as a parameter, it's giving a FiberFailure. Typescript interprets that the type of the error is some class that extends Exception, but in the transpiled code that's not the case. I have logged the value of the error before trying to access to its properties and this is the logging result.

2023-11-23T10:54:59.602Z    b4b0849d-0548-49d1-bf4e-cf4ddfa629a0    INFO    {
  _id: 'FiberFailure',
  cause: {
    _id: 'Cause',
    _tag: 'Fail',
    failure: t {
      visibility: 'private',
      details: [Object],
      _tag: 'ResourceInitializationException'
    }
  }
}


The function is:

export const ErrorLoggerMiddleware: (logger: LoggerProvider) => PostMiddleware = (logger: LoggerProvider) => store => {
  return pipe(
    logger.get(),
    Effect.flatMap(logger =>
      pipe(
        store.getResult(),
        Effect.tapError(failure =>
          pipe(
            {code: failure._tag, details: failure.getDetails()}, //
            ({details: {message, ...details}, code}) => Effect.sync(() => logger.error(message, {code, ...details}))
          )
        ),
        Effect.asUnit
      )
    )
  );
};


I would like to know what am I missing 😅
Was this page helpful?