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

Hello I have a question about casting from error to Moya error? #2329

Open
yongbeomkwak opened this issue Nov 4, 2023 · 5 comments
Open
Labels

Comments

@yongbeomkwak
Copy link

No description provided.

@peril-moya peril-moya bot added the question label Nov 4, 2023
@Gerzer
Copy link

Gerzer commented Nov 14, 2023

What’s your specific question? Do you have a code snippet that illustrates your issue?

@yongbeomkwak
Copy link
Author

yongbeomkwak commented Nov 15, 2023

@Gerzer Hello!
This is where the error occurred.

as you see i tried to convert error to MoyaError but it doesn't work

guard let errorCode = (error as? MoyaError)?.response?.statusCode else {
              // error as? MoyaError is nil
                    if let moyaError = (error as? MoyaError), moyaError.errorCode == 6 {
              
                        return Single.error(api.errorMap[1009] ?? error)
                    }
                    
    return Single.error(error)
}
스크린샷 2023-11-15 오전 9 03 00

@Gerzer
Copy link

Gerzer commented Nov 15, 2023

Swift doesn’t (yet) have typed errors, so the compiler can’t guarantee that all errors that a Moya function might throw will indeed be MoyaError instances. Your code should be able to handle all errors, whether or not they’re MoyaError instances.

@yongbeomkwak
Copy link
Author

@Gerzer Hmm.. Then How Can i use statusCode on error ??

@Gerzer
Copy link

Gerzer commented Nov 29, 2023

@Gerzer Hmm.. Then How Can i use statusCode on error ??

You can only get the statusCode property if the error type is indeed a MoyaError instance. If it’s not a MoyaError instance, then there won’t be any statusCode property.

Example usage:

do {
    // Make a request…
} catch let moyaError as MoyaError {
    let statusCode = moyaError.respone?.statusCode
    // Do something with the status code…
} catch {
    // It’s not a Moya error, so there’s no statusCode property
}

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

No branches or pull requests

2 participants