various bugfixes
This commit is contained in:
@@ -4,35 +4,36 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
/// Send a detailed error report to a Discord webhook
|
||||
pub async fn send_discord_error_report<T: Error>(
|
||||
error: &T,
|
||||
context: Option<&str>, // e.g. provider name, URL, query
|
||||
extra_info: Option<&str>, // any debug info you want
|
||||
use crate::util::requester;
|
||||
|
||||
pub fn format_error_chain(err: &dyn Error) -> String {
|
||||
let mut chain_str = String::new();
|
||||
let mut current_err: Option<&dyn Error> = Some(err);
|
||||
let mut index = 1;
|
||||
while let Some(e) = current_err {
|
||||
let _ = writeln!(chain_str, "{}. {}", index, e);
|
||||
current_err = e.source();
|
||||
index += 1;
|
||||
}
|
||||
chain_str
|
||||
}
|
||||
|
||||
pub async fn send_discord_error_report(
|
||||
error_msg: String,
|
||||
error_chain: Option<String>,
|
||||
context: Option<&str>,
|
||||
extra_info: Option<&str>,
|
||||
file: &str,
|
||||
line: u32,
|
||||
module: &str,
|
||||
) {
|
||||
// 1. Get Webhook from Environment
|
||||
let webhook_url = match std::env::var("DISCORD_WEBHOOK") {
|
||||
Ok(url) => url,
|
||||
Err(_) => return,
|
||||
Err(_) => return, // Exit silently if no webhook is configured
|
||||
};
|
||||
|
||||
// Discord embed field limits
|
||||
const MAX_FIELD: usize = 1024;
|
||||
|
||||
let mut error_chain = String::new();
|
||||
let mut current: &dyn Error = error;
|
||||
let mut i = 0;
|
||||
loop {
|
||||
let _ = writeln!(error_chain, "{}. {}", i + 1, current);
|
||||
i += 1;
|
||||
match current.source() {
|
||||
Some(src) => current = src,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
let truncate = |s: &str| {
|
||||
if s.len() > MAX_FIELD {
|
||||
format!("{}…", &s[..MAX_FIELD - 1])
|
||||
@@ -53,23 +54,23 @@ pub async fn send_discord_error_report<T: Error>(
|
||||
"fields": [
|
||||
{
|
||||
"name": "Error",
|
||||
"value": truncate(&error.to_string()),
|
||||
"value": format!("```{}```", truncate(&error_msg)),
|
||||
"inline": false
|
||||
},
|
||||
{
|
||||
"name": "Error Chain",
|
||||
"value": truncate(&error_chain),
|
||||
"value": truncate(&error_chain.unwrap_or_else(|| "No chain provided".to_string())),
|
||||
"inline": false
|
||||
},
|
||||
{
|
||||
"name": "Location",
|
||||
"value": format!("`{}`:{}\n`{}`", file, line, module),
|
||||
"inline": false
|
||||
"inline": true
|
||||
},
|
||||
{
|
||||
"name": "Context",
|
||||
"value": truncate(context.unwrap_or("n/a")),
|
||||
"inline": false
|
||||
"inline": true
|
||||
},
|
||||
{
|
||||
"name": "Extra Info",
|
||||
@@ -83,13 +84,7 @@ pub async fn send_discord_error_report<T: Error>(
|
||||
}]
|
||||
});
|
||||
|
||||
// Send (never panic)
|
||||
if let Err(e) = wreq::Client::new()
|
||||
.post(webhook_url)
|
||||
.json(&payload)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
eprintln!("Failed to send Discord error report: {e}");
|
||||
}
|
||||
}
|
||||
let mut requester = requester::Requester::new();
|
||||
let _ = requester.post_json(&webhook_url, &payload, vec![]).await;
|
||||
println!("Error report sent.");
|
||||
}
|
||||
Reference in New Issue
Block a user