Fix: Move WS error handling (#174)
Moves all WS handling of unexpected payloads into the stream to prevent code duplication.
This also prevents non-{Hello,Resumed,Ready} messages from causing a handshake failure, as it seems Discord do not prevent such messages from appearing.
---------
Co-authored-by: Kyle Simpson <kyleandrew.simpson@gmail.com>
This commit is contained in:
21
src/ws.rs
21
src/ws.rs
@@ -94,19 +94,16 @@ pub(crate) fn convert_ws_message(message: Option<Message>) -> Result<Option<Even
|
||||
Ok(match message {
|
||||
// SAFETY:
|
||||
// simd-json::serde::from_str may leave an &mut str in a non-UTF state on failure.
|
||||
// The below is safe as we have taken ownership of the inner `String`, and don't
|
||||
// access it as a `str`/`String` or return it if failure occurs.
|
||||
// The below is safe as we have taken ownership of the inner `String`, and if
|
||||
// failure occurs we forcibly re-validate its contents before logging.
|
||||
Some(Message::Text(mut payload)) =>
|
||||
match unsafe { crate::json::from_str(payload.as_mut_str()) } {
|
||||
Ok(event) => Some(event),
|
||||
Err(why) => {
|
||||
debug!(
|
||||
"Could not deserialize websocket event, payload: {}, error: {}",
|
||||
payload, why
|
||||
);
|
||||
None
|
||||
},
|
||||
},
|
||||
(unsafe { crate::json::from_str(payload.as_mut_str()) })
|
||||
.map_err(|e| {
|
||||
let safe_payload = String::from_utf8_lossy(payload.as_bytes());
|
||||
debug!("Unexpected JSON: {e}. Payload: {safe_payload}");
|
||||
e
|
||||
})
|
||||
.ok(),
|
||||
Some(Message::Binary(bytes)) => {
|
||||
return Err(Error::UnexpectedBinaryMessage(bytes));
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user