wasm_ws/error.rs
1// Copyright (c) 2019-2022 Naja Melan
2// Copyright (c) 2023-2024 Yuki Kishimoto
3// Distributed under the MIT software license
4
5use core::str::Utf8Error;
6
7use thiserror::Error;
8
9use crate::CloseEvent;
10
11/// The error type for errors happening in `ws_stream_wasm`.
12//
13#[derive(Debug, Error, Clone, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum WsErr {
16 /// UTF-8 error
17 #[error(transparent)]
18 Utf8(#[from] Utf8Error),
19
20 /// Invalid input to [WsState::try_from( u16 )](crate::WsState).
21 //
22 #[error("Invalid input to conversion to WsReadyState: {supplied}")]
23 //
24 InvalidWsState {
25 /// The user supplied value that is invalid.
26 //
27 supplied: u16,
28 },
29
30 /// When trying to send and [WsState](crate::WsState) is anything but [WsState::Open](crate::WsState::Open) this error is returned.
31 //
32 #[error("The connection state is not \"Open\".")]
33 //
34 ConnectionNotOpen,
35
36 /// An invalid URL was given to [WsMeta::connect](crate::WsMeta::connect), please see:
37 /// [HTML Living Standard](https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket).
38 //
39 #[error("An invalid URL was given to the connect method: {supplied}")]
40 //
41 InvalidUrl {
42 /// The user supplied value that is invalid.
43 //
44 supplied: String,
45 },
46
47 /// An invalid close code was given to a close method. For valid close codes, please see:
48 /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes).
49 //
50 #[error("An invalid close code was given to a close method: {supplied}")]
51 //
52 InvalidCloseCode {
53 /// The user supplied value that is invalid.
54 //
55 supplied: u16,
56 },
57
58 /// The reason string given to a close method is longer than 123 bytes, please see:
59 /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close).
60 //
61 #[error("The reason string given to a close method is to long.")]
62 //
63 ReasonStringToLong,
64
65 /// Failed to connect to the server.
66 //
67 #[error("Failed to connect to the server. CloseEvent: {event:?}")]
68 //
69 ConnectionFailed {
70 /// The close event that might hold extra code and reason information.
71 //
72 event: CloseEvent,
73 },
74
75 /// When converting the JavaScript Message into a WsMessage, it's possible that
76 /// a String message doesn't convert correctly as Js does not guarantee that
77 /// strings are valid Unicode. Happens in `impl TryFrom< MessageEvent > for WsMessage`.
78 //
79 #[error("Received a String message that couldn't be decoded to valid UTF-8")]
80 //
81 InvalidEncoding,
82
83 /// When converting the JavaScript Message into a WsMessage, it's not possible to
84 /// convert Blob type messages, as Blob is a streaming type, that needs to be read
85 /// asynchronously. If you are using the type without setting up the connection with
86 /// [`WsMeta::connect`](crate::WsMeta::connect), you have to make sure to set the binary
87 /// type of the connection to `ArrayBuffer`.
88 ///
89 /// Happens in `impl TryFrom< MessageEvent > for WsMessage`.
90 #[error("Received a Blob message that couldn't converted.")]
91 CantDecodeBlob,
92
93 /// When converting the JavaScript Message into a WsMessage, the data type was neither
94 /// `Arraybuffer`, `String` nor `Blob`. This should never happen. If it does, please
95 /// try to make a reproducible example and file an issue.
96 ///
97 /// Happens in `impl TryFrom< MessageEvent > for WsMessage`.
98 #[error("Received a message that is neither ArrayBuffer, String or Blob.")]
99 UnknownDataType,
100
101 #[error("DOM Exception: {0}")]
102 Dom(u16),
103
104 #[error("{0}")]
105 Other(String),
106}