Test error observer

This commit is contained in:
kozabrada123 2023-08-29 14:44:47 +02:00
parent 2342f6cf7d
commit 98b3d510e4
2 changed files with 12 additions and 4 deletions

View File

@ -2,6 +2,8 @@
use custom_error::custom_error; use custom_error::custom_error;
use reqwest::Error; use reqwest::Error;
use crate::types::WebSocketEvent;
custom_error! { custom_error! {
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub RegistrationError pub RegistrationError
@ -54,9 +56,10 @@ custom_error! {
/// Supposed to be sent as numbers, though they are sent as string most of the time? /// Supposed to be sent as numbers, though they are sent as string most of the time?
/// ///
/// Also includes errors when initiating a connection and unexpected opcodes /// Also includes errors when initiating a connection and unexpected opcodes
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq, Default)]
pub GatewayError pub GatewayError
// Errors we have received from the gateway // Errors we have received from the gateway
#[default]
Unknown = "We're not sure what went wrong. Try reconnecting?", Unknown = "We're not sure what went wrong. Try reconnecting?",
UnknownOpcode = "You sent an invalid Gateway opcode or an invalid payload for an opcode", UnknownOpcode = "You sent an invalid Gateway opcode or an invalid payload for an opcode",
Decode = "Gateway server couldn't decode payload", Decode = "Gateway server couldn't decode payload",
@ -79,3 +82,5 @@ custom_error! {
// Other misc errors // Other misc errors
UnexpectedOpcodeReceived{opcode: u8} = "Received an opcode we weren't expecting to receive: {opcode}", UnexpectedOpcodeReceived{opcode: u8} = "Received an opcode we weren't expecting to receive: {opcode}",
} }
impl WebSocketEvent for GatewayError {}

View File

@ -481,13 +481,15 @@ impl Gateway {
return; return;
} }
// Todo: handle errors in a good way, maybe observers like events?
if msg.is_error() { if msg.is_error() {
warn!("GW: Received error, connection will close.."); let error = msg.error().unwrap();
let _error = msg.error(); warn!("GW: Received error {:?}, connection will close..", error);
self.close().await; self.close().await;
self.events.lock().await.error.notify(error).await;
return; return;
} }
@ -937,6 +939,7 @@ mod events {
pub webhooks: Webhooks, pub webhooks: Webhooks,
pub gateway_identify_payload: GatewayEvent<types::GatewayIdentifyPayload>, pub gateway_identify_payload: GatewayEvent<types::GatewayIdentifyPayload>,
pub gateway_resume: GatewayEvent<types::GatewayResume>, pub gateway_resume: GatewayEvent<types::GatewayResume>,
pub error: GatewayEvent<GatewayError>,
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]