pub struct Error { /* private fields */ }web03 only.Expand description
Error type for the WebSocket service.
Errors are delivered to request, channel and broadcast callbacks, and to
the connection-wide handler set with ServiceBuilder::on_error. Use the
accessors below to tell what failed instead of matching on the text
produced by Display, which is meant for humans and may change:
is_not_connected- the request was never sent, since there was no open connection or channel to send it over. It is safe to send it again once the connection is open.is_connection_lost- the request was sent, or about to be, but the connection closed before a response arrived. The server may or may not have handled it.is_cancelled- the request was replaced by another one before it was answered.as_server_error- the server answered with an error, such as one returned by its handler. If the handler failed with a typed error,server_errordecodes it.is_decode- something received could not be decoded, such as a response which does not match the expected type.
Anything else, such as a failure in the browser APIs, only carries a message.
§Examples
use musli_web::web::Error;
fn describe(error: &Error) -> String {
if error.is_not_connected() {
String::from("Not connected, try again later")
} else if error.is_connection_lost() {
String::from("Connection lost, the request may not have been handled")
} else if let Some(message) = error.as_server_error() {
format!("The server said: {message}")
} else {
error.to_string()
}
}
let error = Error::message("Something else");
assert_eq!(describe(&error), "Something else");Implementations§
Source§impl Error
impl Error
Sourcepub fn is_empty_packet(&self) -> bool
pub fn is_empty_packet(&self) -> bool
Check if the error is caused by an empty packet.
An empty packet is also a decode error.
§Examples
use musli_web::web::{Error, RawPacket};
let packet = RawPacket::empty();
let e = packet.decode::<u32>().unwrap_err();
assert!(e.is_empty_packet());
assert!(e.is_decode());Sourcepub fn is_not_connected(&self) -> bool
pub fn is_not_connected(&self) -> bool
Check if the request was never sent, since there was no open connection or channel to send it over.
This is the error a request or channel gets when it is sent before the
connection is open, after the Service has been
dropped, or over a Channel which is not open. Since the server never
saw the request, it is safe to send it again once the connection is
open, see Handle::on_state_change.
§Examples
use musli_web::web::Error;
let error = Error::message("Something else");
assert!(!error.is_not_connected());Sourcepub fn is_connection_lost(&self) -> bool
pub fn is_connection_lost(&self) -> bool
Check if the connection closed before a response to the request arrived.
Unlike is_not_connected, the request was sent, or was being sent,
so the server may or may not have handled it. This is the error every
pending request gets when the connection closes or when the
Service is dropped.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if the request was cancelled before it was answered, since another request took its place.
Sourcepub fn as_server_error(&self) -> Option<&str>
pub fn as_server_error(&self) -> Option<&str>
Check if the error is a server error, and if so return the message reported by the server.
Server errors are produced by a Handler which returns an error or
which does not support the request or broadcast being made. The message
is returned as the server sent it, without the Server error: prefix
that Display adds.
This is the message of a typed error too.
Sourcepub fn server_error<'de, T>(&'de self) -> Result<Option<T>, Error>where
T: DecodeBody<'de>,
pub fn server_error<'de, T>(&'de self) -> Result<Option<T>, Error>where
T: DecodeBody<'de>,
Decode the typed error the server answered with.
A handler which fails with a ws::TypedError<E> sends E encoded in
the body format of the connection, see Errors. This decodes it back
as T, which should be the same type as E or one which is
compatible with it, typically from an API crate shared with the server.
Returns Ok(None) if this is not a server error, or if it is one which
carries no typed error, such as a handler error which is plain text or
an error the server produced on its own for a request it does not
support. Those still have a message, see as_server_error.
§Errors
Errors if the typed error does not decode as T. The returned error is
a decode error.
§Examples
use musli::{Decode, Encode};
use musli_web::web::Error;
#[derive(Debug, PartialEq, Encode, Decode)]
enum ServerError {
NotFound,
Forbidden,
}
fn describe(error: &Error) -> String {
match error.server_error::<ServerError>() {
Ok(Some(ServerError::NotFound)) => String::from("No such thing"),
Ok(Some(ServerError::Forbidden)) => String::from("Not allowed"),
// NB: Not a typed error, or not one we understand.
Ok(None) | Err(..) => error.to_string(),
}
}
let error = Error::message("Something else");
assert_eq!(error.server_error::<ServerError>().unwrap(), None);
assert_eq!(describe(&error), "Something else");Sourcepub fn is_decode(&self) -> bool
pub fn is_decode(&self) -> bool
Check if the error is caused by something received which could not be decoded.
This covers a response or broadcast whose body does not decode as the requested type, an empty packet, a frame from the server whose header, error message or format could not be decoded, and a typed error which does not decode as the requested type.
§Examples
use musli_web::web::RawPacket;
let packet = RawPacket::empty();
let e = packet.decode::<u32>().unwrap_err();
assert!(e.is_decode());Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Auto Trait Implementations§
impl Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
impl UnwindSafe for Error
Blanket Implementations§
impl<Token, Builder, How> AllPropsFor<Builder, How> for Tokenwhere
Builder: Buildable<Token>,
<Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,
impl<Token, Builder, How> AllPropsFor<Builder, How> for Tokenwhere
Builder: Buildable<Token>,
<Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> HasAllProps<(), T> for T
impl<T> HasAllProps<(), T> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
Source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self to a value of a Properties struct.Source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
Source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self to a value of a Properties struct.Source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
Source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self to a value of a Properties struct.Source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
Source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self to a value of a Properties struct.impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<E> ResponseError for E
impl<E> ResponseError for E
Source§fn fmt_message(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt_message(&self, f: &mut Formatter<'_>) -> Result<(), Error>
ws only.Source§fn encode_payload(&self, payload: ErrorPayload<'_>) -> Result<(), Error>
fn encode_payload(&self, payload: ErrorPayload<'_>) -> Result<(), Error>
ws only.ErrorPayload::encode. Read more