Replies: 2 comments 4 replies
-
I'm also interested in the question, lately I've been conforming Action to Equatable only in test modules like. extension Action: Equatable {
public static func == (lhs: Action, rhs: Action) -> Bool {
String(reflecting: lhs) == String(reflecting: rhs)
}
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for the question! It's a good one! We consider our As soon as you care about the error information, we would not recommend using We recommend introducing domain-specific error types such as these whenever you want to start handling errors in a nuanced way. You may still need to be careful when handling errors that come from outside your domain in order to translate them into the correct errors in your domain. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In the examples in the TCA repository, I sometimes see examples of casting Error to NSError for comparison.
However, can we trust this method?
This is because different values may be treated as the same, as shown below.
When you want to have an Error in an Action, you mainly want to make it conform to Equatable at test time!
In this case, I thought it would be better to initialize the NSError directly, instead of the error defined in Swift.
Like this:
swift-composable-architecture/Tests/ComposableArchitectureTests/EffectTests.swift
Line 190 in 96cdc7e
If you want to have Error in State, you want to make it conform to Equatable when using WithViewStore or when testing.
This may not be a problem when testing (In this case, we can initialize NSError directly), but in a production implementation, the Error defined in Swift may be cast to an NSError and
As shown above, it may not be the same, but it may be determined to be the same.
If this happens, the View may not be redrawn.
In such a case, a possible approach is to not have Error in the State, and only keep the necessary data.
Another way is to wrap the Error.
However, this is not available in the Result, so the Wrapper will also want to conform to Error.
However, since the ErrorWrapper is not an actual error, you need to handle it by taking out the underlyingError.
You'll need to be careful if you want to present a message, for example.
As you can see, there are various considerations regarding Error and Equatable.
I'm sure each app is different in how it should be handled, but if someone have any recommendations or ideas, please let me know.
Beta Was this translation helpful? Give feedback.
All reactions