use core::fmt;
use musli_web05::ws::{self, ErrorPayload, ResponseError};
use crate::core::Error;
use crate::store::StoreError;
use crate::types::AuthError;
#[derive(Debug)]
pub struct RequestError(Error);
impl RequestError {
pub fn error(&self) -> &Error {
&self.0
}
pub fn into_error(self) -> Error {
self.0
}
pub fn auth_error(&self) -> Option<&AuthError> {
self.0.auth_error()
}
}
impl ResponseError for RequestError {
fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Error::Auth(error) => fmt::Display::fmt(error, f),
_ => f.write_str("internal error"),
}
}
fn encode_payload(&self, payload: ErrorPayload<'_>) -> Result<(), ws::Error> {
match &self.0 {
Error::Auth(error) => payload.encode(error),
_ => Ok(()),
}
}
}
impl From<Error> for RequestError {
fn from(error: Error) -> Self {
Self(error)
}
}
impl From<AuthError> for RequestError {
fn from(error: AuthError) -> Self {
Self(Error::Auth(error))
}
}
impl From<StoreError> for RequestError {
fn from(error: StoreError) -> Self {
Self(Error::Store(error))
}
}