-
Notifications
You must be signed in to change notification settings - Fork 8
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
Handle parsing exceptions #55
Comments
That solution sounds good to me. |
I've been thinking about an alternative pattern that would maybe be simpler to use. It would return a list of results that would still be in order and each result would contain either success or an exception. This would probably support other endpoints more easily. Pattern 2: class ModelException {
final Exception exception;
final String unparsed;
ModelException({
required this.exception,
required this.unparsed,
});
}
class Result<T> {
final T? data;
final ModelException? error;
Result(this.data, this.error);
}
client.safeParse = true;
final List<Result<Status>> response = await client.timeline(); A third pattern I was thinking about was support a builder method where users could support custom parsing/models. Pattern 3: var response = await client.timeline(
modelBuilder: (json) {
try {
return Status.fromJson(json);
} catch (e) {
return ModelException(
exception: e as Exception,
unparsed: json.toString(),
);
}
},
); Currently I'm leaning towards pattern 2. |
I almost suggested pattern 2 because it would be nice to preserve the order, especially giving developers the option to show a failed to load widget if they so desire. That said, I don't really like any of Dart's ways to handle multiple cases for a single value, since we don't have tagged unions etc. I think pattern 2 is the cleanest approach that preserves order. |
I expect API response formats to become more divergent and buggy as more servers and forks of Mastodon popup. Currently if you request a timeline and one posts triggers a parsing exception (like #51) the entire response is lost. I'd like to see a way to get all the successful statuses and a list of exceptions for the failed statuses. This way users can be shown most of the content and the option to report parse issues to developers.
I'm noodling something similar to the following. What do you think?
Pattern 1:
The text was updated successfully, but these errors were encountered: