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

Make it possible to catch specific error types and chain error handling #65

Open
gwynne opened this Issue Feb 6, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@gwynne
Copy link

gwynne commented Feb 6, 2018

Example:

func getUser(req: Request) -> Future<ResponseRepresentable> {
    return User.find(req.parameters.next()).map(to: PublicUser.self) { user in
        return try user.makePublic()
    }.catchMap(UserError.self, to: Response.self) { error in
        guard case .makePublicFailed = error else { throw error } 
        
        return req.makeResponse().withEncodedContent(MyErrorResponse())
    }
}

Of particular interest is how to handle catchMap() blocks returning a different type than the last map() block.

@calebkleveter

This comment has been minimized.

Copy link
Member

calebkleveter commented Feb 9, 2018

Why call it catchMap? If I am correct, we want to mirror do/catch syntax so it feels more natural. I am more in favor of changing (or overloading) the current catch method to handle throwing and returning.

@mcdappdev

This comment has been minimized.

Copy link

mcdappdev commented Feb 9, 2018

I actually prefer catchMap. It still returns a value (in @gwynne's example, return req.makeResponse().withEncodedContent(MyErrorResponse())) which I think is reason enough to let it follow the format of the current vapor map and flatMap methods. Especially if it's going to have similar overloads like to: .

Regular catch might get confusing with Vapor's catch, as the Swift catch doesn't explicitly return a value.

@calebkleveter

This comment has been minimized.

Copy link
Member

calebkleveter commented Feb 9, 2018

Correct, it doesn't return a value, but I can return a value from a method from inside the catch block (or throw, for that matter).

@mcdappdev

This comment has been minimized.

Copy link

mcdappdev commented Feb 9, 2018

Of course, but the method proposed here requires some kind of return, since it's chained on to the map or flatMap above it. Personally I think it's clearer when it follows the same pattern

@kdawgwilk

This comment has been minimized.

Copy link

kdawgwilk commented Feb 9, 2018

I would prefer to keep things as similar to RxSwift as we can so the transition is easy for iOS devs using RxSwift to come over to Vapor 3: catchError && catchErrorJustReturn

@tanner0101 tanner0101 self-assigned this Feb 23, 2018

@tanner0101 tanner0101 added this to Backlog in Vapor 3 Feb 26, 2018

@tanner0101 tanner0101 removed this from Backlog in Vapor 3 Mar 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment