Add StringInterpolation helpers to render errors#37
Conversation
|
@finestructure First of all, thank you very much for the suggestion and for taking the time to implement! 👍 I like the general idea of adding string interpolation helpers to save some typing. If I understand this correctly, this is mainly a convenience addition that reduces something like this: print(ErrorKit.userFriendlyMessage(for: error))
// or
Logger().error("Error Chain: \(ErrorKit.errorChainDescription(for: error))")To something shorter like this: print("\(error: error)")
// or
Logger().error("Error Chain: \(errorChain: error)")While I'm personally not sure if this is something I would prefer (because brevity does not always lead to more readable code), I love the idea to support Swift-native features like string interpolation as long as it's optional and the developer can choose to use it if they find it more readable depending on their use case. But I'm not sure if we should use the word I would much rather use some wording that is specific to ErrorKit and makes it easier to discover where it's coming from. Potential naming ideas:
I personally would prefer approach 2, combined with The end result would be an API like this: print("\(userFriendlyMessage: error)")
// or
Logger().error("Error Chain: \(errorChainDescription: error)")@finestructure Let me know what you think! |
|
While I agree that brevity does not always lead to more readable code, I believe here it does. The extra nesting and verbosity of having to call via the full static type name make this a very "heavy" expression. In short strings, the expression is dominated by the function signature and in longer strings it'll quickly lead to awkward line breaks. With respect to argument labels, ideally I would have proposed not to use labels at all. (As noted, this would work for I think the reason it doesn't need an argument label other than to deal with overload resolution is that the interpolation essentially always does the right thing. There's nothing unsafe or unexpected about what it does, so I don't think it needs any special signalling at the call site. You have an error, it gets printed. And now in a nicer way :) These extensions can easily be adopted client side, so please don't hesitate to close the PR if you don't feel it's a good fit! |
|
@finestructure Thanks again for suggesting this feature. I thought about this more and agree that developers explicitly choosing to use ErrorKit probably want improved error messages everywhere they can get them. So I decided to actually make it work like this for any print("Updated failed: \(error)") // calls ErrorKit.userFriendlyMessage(for:) internally
print("Update failed: \(chain: error)") // calls ErrorKit.errorChainDescription(for:) internally
print("Update failed: \(debug: error)") // alias for chain: – therefore also calls errorChainDescriptionI think this is going to be the most convenient way of printing errors and it should even automatically improve the behavior of logging just by adding ErrorKit without changing a line of code in your code base. I'm gonna merge this PR as is so you get credited on GitHub history, gonna do the changes after. 🎉 |
As discussed on Mastodon, this adds support for interpolating errors:
The argument label
error:is required here or it'll break existing interpolation ofSwift.Error. Alternatively (or additionally), there could be an overloadwithout the label but it's probably better to keep it symmetrical with
errorChain:.