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