Test error observer

This commit is contained in:
kozabrada123 2023-08-29 14:44:47 +02:00
parent 5a4d3cba04
commit 3b3ba4f3cf
2 changed files with 12 additions and 4 deletions

View File

@ -2,6 +2,8 @@
use custom_error::custom_error;
use reqwest::Error;
use crate::types::WebSocketEvent;
custom_error! {
#[derive(PartialEq, Eq)]
pub RegistrationError
@ -54,9 +56,10 @@ custom_error! {
/// 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
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Default)]
pub GatewayError
// Errors we have received from the gateway
#[default]
Unknown = "We're not sure what went wrong. Try reconnecting?",
UnknownOpcode = "You sent an invalid Gateway opcode or an invalid payload for an opcode",
Decode = "Gateway server couldn't decode payload",
@ -79,3 +82,5 @@ custom_error! {
// Other misc errors
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;
}
// Todo: handle errors in a good way, maybe observers like events?
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.events.lock().await.error.notify(error).await;
return;
}
@ -937,6 +939,7 @@ mod events {
pub webhooks: Webhooks,
pub gateway_identify_payload: GatewayEvent<types::GatewayIdentifyPayload>,
pub gateway_resume: GatewayEvent<types::GatewayResume>,
pub error: GatewayEvent<GatewayError>,
}
#[derive(Default, Debug)]