|
| 1 | +use http::StatusCode; |
1 | 2 | use thiserror::Error; |
2 | 3 |
|
3 | | -/// [`Result`](std::result::Result) type that is returned from functions with error |
4 | | -/// as [`TypesenseError`](crate::error::TypesenseError). |
| 4 | +/// [`Result`](std::result::Result) type that is returned from |
| 5 | +/// functions with error as [`TypesenseError`]. |
5 | 6 | pub type Result<T> = std::result::Result<T, TypesenseError>; |
6 | 7 |
|
7 | 8 | /// Represents an error that can occur while using the library. |
8 | 9 | #[derive(Error, Debug)] |
9 | 10 | pub enum TypesenseError { |
| 11 | + /// TypesenseClientError |
| 12 | + #[error("typesense client error")] |
| 13 | + TypesenseClientError, |
| 14 | + |
10 | 15 | /// Config error. |
11 | 16 | #[error("config error")] |
12 | 17 | ConfigError, |
@@ -50,4 +55,51 @@ pub enum TypesenseError { |
50 | 55 | /// HTTP status error. |
51 | 56 | #[error("HTTP status error")] |
52 | 57 | HttpStatusError, |
| 58 | + |
| 59 | + /// HTTP error. |
| 60 | + #[error("http error: {0}")] |
| 61 | + HttpError(#[from] http::Error), |
| 62 | + |
| 63 | + /// Hyper error. |
| 64 | + #[cfg(not(target_arch = "wasm32"))] |
| 65 | + #[cfg_attr(docsrs, doc(cfg(not(target_arch = "wasm32"))))] |
| 66 | + #[error("hyper error: {0}")] |
| 67 | + HyperError(#[from] hyper::Error), |
| 68 | + |
| 69 | + /// WASM error. |
| 70 | + #[cfg(target_arch = "wasm32")] |
| 71 | + #[cfg_attr(docsrs, doc(cfg(target_arch = "wasm32")))] |
| 72 | + #[error("wasm client error: {0:?}")] |
| 73 | + WasmError(String), |
| 74 | +} |
| 75 | + |
| 76 | +impl From<StatusCode> for TypesenseError { |
| 77 | + fn from(status: StatusCode) -> Self { |
| 78 | + match status { |
| 79 | + // 400 |
| 80 | + StatusCode::BAD_REQUEST => Self::RequestMalformed, |
| 81 | + // 401 |
| 82 | + StatusCode::UNAUTHORIZED => Self::RequestUnauthorized, |
| 83 | + // 403 |
| 84 | + StatusCode::FORBIDDEN => Self::RequestForbidden, |
| 85 | + // 404 |
| 86 | + StatusCode::NOT_FOUND => Self::ObjectNotFound, |
| 87 | + // 409 |
| 88 | + StatusCode::CONFLICT => Self::ObjectAlreadyExists, |
| 89 | + // 422 |
| 90 | + StatusCode::UNPROCESSABLE_ENTITY => Self::ObjectUnprocessable, |
| 91 | + // 500 |
| 92 | + StatusCode::INTERNAL_SERVER_ERROR => Self::ServerError, |
| 93 | + // 503 |
| 94 | + StatusCode::SERVICE_UNAVAILABLE => Self::ServiceUnavailable, |
| 95 | + _ => Self::TypesenseClientError, |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +#[cfg(target_arch = "wasm32")] |
| 101 | +impl From<wasm_bindgen::JsValue> for TypesenseError { |
| 102 | + fn from(value: wasm_bindgen::JsValue) -> Self { |
| 103 | + Self::WasmError(format!("{:?}", value)) |
| 104 | + } |
53 | 105 | } |
0 commit comments