webparse/
error.rs

1// Copyright 2022 - 2023 Wenmeng See the COPYRIGHT
2// file at the top-level directory of this distribution.
3// 
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8// 
9// Author: tickbh
10// -----
11// Created Date: 2023/08/15 10:47:56
12
13use std::{fmt::{self}, result, convert::Infallible};
14
15use crate::{http::HttpError, url::UrlError, Http2Error, ws::WsError};
16
17#[derive(Debug)]
18pub enum WebError {
19    Http(HttpError),
20    Http2(Http2Error),
21    Ws(WsError),
22    Url(UrlError),
23    IntoError,
24    Extension(&'static str),
25    Serialize(&'static str),
26    Io(std::io::Error),
27}
28
29impl WebError {
30    #[inline]
31    fn description_str(&self) -> &'static str {
32        match self {
33            WebError::Url(e) => e.description_str(),
34            WebError::Http(e) => e.description_str(),
35            WebError::Http2(e) => e.description_str(),
36            WebError::Ws(e) => e.description_str(),
37            WebError::IntoError => "into value error",
38            WebError::Extension(_) => "std error",
39            WebError::Serialize(_) => "serialize error",
40            WebError::Io(_) => "io error",
41            
42        }
43    }
44
45    pub fn is_partial(&self) -> bool {
46        match self {
47            WebError::Http(HttpError::Partial) => true,
48            _ => false
49        }
50    }
51}
52
53impl fmt::Display for WebError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        f.write_str(self.description_str())
56    }
57}
58
59impl From<std::num::ParseIntError> for WebError {
60    fn from(_: std::num::ParseIntError) -> Self {
61        WebError::Extension("parse int error")
62    }
63}
64
65impl From<std::io::Error> for WebError {
66    fn from(e: std::io::Error) -> Self {
67        WebError::Io(e)
68    }
69}
70
71impl From<HttpError> for WebError {
72    fn from(e: HttpError) -> Self {
73        WebError::Http(e)
74    }
75}
76
77impl From<UrlError> for WebError {
78    fn from(e: UrlError) -> Self {
79        WebError::Url(e)
80    }
81}
82
83impl From<Infallible> for WebError {
84    fn from(_: Infallible) -> Self {
85        WebError::Extension("Infallible")
86    }
87}
88
89pub type WebResult<T> = result::Result<T, WebError>;