-
Notifications
You must be signed in to change notification settings - Fork 15
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
Conversation
Adds two static functions to `XPCError` one which generates it from an `Error` and the other from an `xpc_object_t`.
@amomchilov for your review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
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 | ||
} |
There was a problem hiding this comment.
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
:
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 | |
} |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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 | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Adds two static functions to
XPCError
one which generates it from anError
and the other from anxpc_object_t
.