Skip to main content

Error

Struct Error 

Source
pub struct Error { /* private fields */ }
Available on crate feature 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_error decodes 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

Source

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());
Source

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());
Source

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.

Source

pub fn is_cancelled(&self) -> bool

Check if the request was cancelled before it was answered, since another request took its place.

Source

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.

Source

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");
Source

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());
Source

pub fn message(message: impl Display) -> Self

Format a WebSocket error consisting of a message.

None of the accessors on Error match an error constructed this way.

Trait Implementations§

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<JsValue> for Error

Source§

fn from(error: JsValue) -> Self

Converts to this type from the input type.

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§

Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HasAllProps<(), T> for T

Source§

impl<T> HasAllProps<(), T> for T

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<E> ResponseError for E
where E: 'static + Send + Display,

Source§

fn fmt_message(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Available on crate feature ws only.
Write the human readable message of the error. Read more
Source§

fn encode_payload(&self, payload: ErrorPayload<'_>) -> Result<(), Error>

Available on crate feature ws only.
Encode the typed payload of the error, if it has one, by calling ErrorPayload::encode. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more