Skip to content

Handling multiple error types. #38

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

Merged
merged 7 commits into from
May 25, 2015
Merged

Handling multiple error types. #38

merged 7 commits into from
May 25, 2015

Conversation

inamiy
Copy link
Member

@inamiy inamiy commented May 24, 2015

This pull request will fix #37 by:

  • improving type-inference for overloaded then()/success()/failure() to not use the wrong one
  • using then() to promote Task type from Task<P1, V1, E1> to Task<P2, V2, E2> (including E1 -> E2 Error type change)

For more information, please see MultipleErrorTypesTests.swift.

Caveat

let task1: Task1 = self.request1()
task1.success { _ -> Task2 in
    return self.request2() // let's say, this task will fail
}.then { value, errorInfo -> Void in
    // NOTE:
    // Though `errorInfo` will still be non-nil, `errorInfo!.error` will become as `nil`
    // if Task1 and Task2 have different Error types.
}

If Task1 and Task2 have different Error types and request2 fails,
you will still receive errorInfo as non-nil, but now errorInfo!.error will become as nil.

This is because there is no way to convert request2's error back to task1's Error type (which is required in success() task-flow).

To avoid this default behavior, it is user's job to add error-conversion logic to maintain same Error type throughout the task-flow, by adding another type-promotable then()/success()/failure().

Sorry, something went wrong.

@inamiy inamiy added the bug label May 24, 2015
@inamiy inamiy changed the title Multiple error types Handling multiple error types. May 24, 2015
@inamiy inamiy added this to the 3.3.0 milestone May 24, 2015
@tokorom
Copy link
Contributor

tokorom commented May 25, 2015

@inamiy LGTM 💯

SwiftTask became stronger and I love deepened it :)

@inamiy
Copy link
Member Author

inamiy commented May 25, 2015

@tokorom
Thank you for catching this nasty bug!
Error mapping was really a needed feature, so I appreciate your reporting issue 😉

I will add a bit more comments and tests before bumping to 3.3.0.

@inamiy
Copy link
Member Author

inamiy commented May 25, 2015

I added fac9ff6 to demonstrate how to avoid trappy Error conversion.
Please check updated top comment as well.

Ready to :shipit:

inamiy added a commit that referenced this pull request May 25, 2015
@inamiy inamiy merged commit 9ffc9d6 into master May 25, 2015
@inamiy inamiy deleted the multiple-error-types branch May 25, 2015 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants