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

Consolidates XPCError generation logic #62

Merged
merged 1 commit into from
Jan 21, 2022
Merged

Conversation

jakaplan
Copy link
Contributor

Adds two static functions to XPCError one which generates it from an Error and the other from an xpc_object_t.

Adds two static functions to `XPCError` one which generates it from an `Error` and the other from an `xpc_object_t`.
@jakaplan
Copy link
Contributor Author

@amomchilov for your review

Copy link
Contributor

@amomchilov amomchilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Comment on lines +81 to +91
if let error = error as? XPCError {
return error
} else if let error = error as? DecodingError {
return .decodingError(String(describing: error))
} else if let error = error as? EncodingError {
return .encodingError(String(describing: error))
} else if expectingOtherError {
return .other(String(describing: error))
} else {
return .unknown
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another case of being more idiomatic with a switch:

Suggested change
if let error = error as? XPCError {
return error
} else if let error = error as? DecodingError {
return .decodingError(String(describing: error))
} else if let error = error as? EncodingError {
return .encodingError(String(describing: error))
} else if expectingOtherError {
return .other(String(describing: error))
} else {
return .unknown
}
switch error {
case let error as? XPCError: return error
case let error as? DecodingError: return .decodingError(String(describing: error))
case let error as? EncodingError: return .encodingError(String(describing: error))
case _ where expectingOtherError: return .other(String(describing: error))
case _: return .unknown
}

Copy link
Contributor Author

@jakaplan jakaplan Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, why is this more idiomatic? (I mean this genuinely, not rhetorically.) The last two cases seem rather oddly constructed where a _ is needed; particularly the penultimate one case _ where expectingOtherError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll commit this as-is, but happy to revise it later

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a general trend in Swift code to prefer switch over if/else, because it makes it more clear that the predicates are all related (in that they're checking against the same operand).

A preference one way or the other isn't explicitly documented in any of the style guides I know, but some rationale is explained well here (in the context of C++, where their switch statements are waaaaaaay less powerful): https://youtu.be/raB_289NxBk?t=287

Granted, the last 2 cases here muddy the water a bit, though nonetheless I think they communicate intent very well

Comment on lines +95 to +105
internal static func fromXPCObject(_ object: xpc_object_t) -> XPCError {
if xpc_equal(object, XPC_ERROR_CONNECTION_INVALID) {
return .connectionInvalid
} else if xpc_equal(object, XPC_ERROR_CONNECTION_INTERRUPTED) {
return .connectionInterrupted
} else if xpc_equal(object, XPC_ERROR_TERMINATION_IMMINENT) {
return .terminationImminent
} else {
return .unknown
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do a fair bit of if/else checking with xpc_equal, do you think it's worth introducing a matcher that implements ~=, so we can do something like this? https://stackoverflow.com/a/54487077/3141234

Copy link
Contributor Author

@jakaplan jakaplan Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this consolidation there are 7 total calls to xpc_equal in the codebase, of which this is 3 of them. So no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's nice. I remember there being more of them, I guess this fixed a lot of them.

@jakaplan jakaplan merged commit 37557ee into main Jan 21, 2022
@jakaplan jakaplan deleted the consolidated-error-logic branch January 21, 2022 02:55
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

Successfully merging this pull request may close these issues.

None yet

2 participants