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

MoyaError does not include original URLRequest in all cases #2286

Open
kamendo opened this issue Oct 17, 2022 · 1 comment
Open

MoyaError does not include original URLRequest in all cases #2286

kamendo opened this issue Oct 17, 2022 · 1 comment

Comments

@kamendo
Copy link

kamendo commented Oct 17, 2022

Hi,
My problem is that convertResponseToResult function does not always include the original URLRequest
So if the error is "No internet" we will get
MoyaError.underlying(error, nil), i.e. the error is missing the context of the original request which in my case is important

For example in my custom plugin, "didReceive(..." when I receive this error I get very limited information about the original request (I can only get some info from TargetType object) and I am unable to match it with willSend function

Please consider updating the source so that on all errors we have attached the original request

@ljluestc
Copy link

import Moya
import Combine

extension MoyaError {
    static func withRequest(_ request: URLRequest, error: Error) -> MoyaError {
        return .underlying(error, request)
    }
}

extension Publisher where Output == Response, Failure == MoyaError {
    func convertResponseToResult() -> AnyPublisher<Result<Response, MoyaError>, Never> {
        return map { .success($0) }
            .catch { error in
                guard let moyaError = error as? MoyaError else {
                    return Just(.failure(.underlying(error, nil))).eraseToAnyPublisher()
                }
                
                switch moyaError {
                case .underlying(let underlyingError, _):
                    if let urlRequest = underlyingError as? URLRequest {
                        return Just(.failure(MoyaError.withRequest(urlRequest, error: moyaError))).eraseToAnyPublisher()
                    } else {
                        return Just(.failure(moyaError)).eraseToAnyPublisher()
                    }
                default:
                    return Just(.failure(moyaError)).eraseToAnyPublisher()
                }
            }
            .eraseToAnyPublisher()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants