Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help converting error types #145

Open
berbsd opened this issue Jan 14, 2024 · 1 comment
Open

Help converting error types #145

berbsd opened this issue Jan 14, 2024 · 1 comment
Labels
question Asking about fpdart and functional programming

Comments

@berbsd
Copy link

berbsd commented Jan 14, 2024

I’m struggling to see how I can convert from one error type to another.

I have a function that returns a TaskEither<ApiError, Client>. I want to use the resulting client in another function that returns a TaskEither<ServiceError, String> . The two error types are not sharing a common base class.

I tried to chain the calls but could not make it work. The code is something along these lines. Also tried to add a mapLeft but did not work either. I have been going in circles and any help would be greatly appreciated.

The library is awesome, but would love to see more examples for common use-cases.

TaskEither<ServiceError, String> apiCall(URI uri) {
  
  return getClient()
      .mapLeft((error) => ServiceError.mapError(error))
      .flatMap((client) => TaskEither.tryCatch(
                () async => client.get(uri),
                ServiceError.mapError
              ).chainEither( // …. Remaining code to parse response

}

// the signature for getClient is as follow

TaskEither<ApiError, String> getClient() {
 …
}
@SandroMaglione
Copy link
Owner

Hi @berbsd

How is ServiceError.mapError defined?

mapLeft((error) => ServiceError.mapError(error)) should convert ApiError to ServiceError for this to work, for example:

class ServiceError {
  ServiceError();
  factory ServiceError.mapError(ApiError error) => ServiceError();
}

The second ServiceError.mapError inside tryCatch cannot have the same signature, since the error in tryCatch gives you Object + StackTrace instead of ApiError.

If for example you refactor like below it compiles:

typedef URI = String;

class ServiceError {
  ServiceError();
  factory ServiceError.mapError(ApiError error) => ServiceError();
}

class ApiError {}

TaskEither<ApiError, String> getClient = TaskEither.of("");
TaskEither<ServiceError, String> apiCall(URI uri) {
  return getClient
      .mapLeft(
        (error) => ServiceError.mapError(error),
      )
      .flatMap(
        (client) => TaskEither.tryCatch(
          () async => client + "",
          (_, __) => ServiceError(),
        ),
      );
}

Let me know if you have any question

@SandroMaglione SandroMaglione added the question Asking about fpdart and functional programming label Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking about fpdart and functional programming
Projects
None yet
Development

No branches or pull requests

2 participants