Actually provide meaningful errors

This commit is contained in:
Maybe Waffle 2023-02-01 17:11:07 +04:00
parent cbbfc5b3d1
commit 2221116d9c

View file

@ -102,7 +102,7 @@ where
serde_json::from_str::<TelegramResponse<T>>(&text)
.map(|mut response| {
use crate::types::{Update, UpdateKind};
use std::any::Any;
use std::{any::Any, iter::zip};
// HACK: Fill-in error information into `UpdateKind::Error`.
//
@ -124,13 +124,24 @@ where
// `UpdateKind::Error(/* some empty-ish value */)`. Here, through some
// terrible hacks and downcasting, we fill-in the data we couldn't parse
// so that our users can make actionable bug reports.
if TypeId::of::<T>() == TypeId::of::<Update>() {
//
// We specifically handle `Vec<Update>` here, because that's the return
// type of the only method that returns updates.
if TypeId::of::<T>() == TypeId::of::<Vec<Update>>() {
if let TelegramResponse::Ok { response, .. } = &mut response {
if let Some(update) =
(response as &mut T as &mut dyn Any).downcast_mut::<Update>()
if let Some(updates) =
(response as &mut T as &mut dyn Any).downcast_mut::<Vec<Update>>()
{
if let UpdateKind::Error(value) = &mut update.kind {
*value = serde_json::from_str(&text).unwrap_or_default();
if updates.iter().any(|u| matches!(u.kind, UpdateKind::Error(_))) {
let re_parsed = serde_json::from_str(&text);
if let Ok(TelegramResponse::Ok { response: values, .. }) = re_parsed {
for (update, value) in zip::<_, Vec<_>>(updates, values) {
if let UpdateKind::Error(dest) = &mut update.kind {
*dest = value;
}
}
}
}
}
}