toResult method
Awaits the response and folds it into a Result.
On response.isSuccessful and a non-null body, returns Success.
Otherwise builds a Failure via failureMapper (defaults to
defaultChopperFailureMapper).
Implementation
Future<Result<BodyType>> toResult({
ChopperFailureMapper<BodyType>? failureMapper,
}) async {
try {
final response = await this;
if (response.isSuccessful) {
final body = response.body;
if (body == null) {
return Error<BodyType>(
const Failure.parsing(message: 'Empty response body'),
);
}
return Success<BodyType>(body);
}
final mapper = failureMapper ?? defaultChopperFailureMapper<BodyType>;
return Error<BodyType>(mapper(response));
} catch (e, st) {
return Error<BodyType>(
Failure.unknown(message: e.toString(), cause: e, stackTrace: st),
);
}
}