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