tauri_plugin_http/error.rs
1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use serde::{Serialize, Serializer};
6use url::Url;
7
8/// Errors that can happen while using the HTTP plugin.
9///
10/// The error is serialized to the frontend as its [`Display`](std::fmt::Display) string.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13 /// JSON serialization or deserialization error.
14 #[error(transparent)]
15 Json(#[from] serde_json::Error),
16 /// File system error, raised for instance when the cookie store file cannot be created or
17 /// opened on startup.
18 #[error(transparent)]
19 Io(#[from] std::io::Error),
20 /// Error from the underlying [`reqwest`] client, raised while building the client, sending the
21 /// request or reading the response body.
22 #[error(transparent)]
23 Network(#[from] reqwest::Error),
24 /// Error from the [`http`] crate, raised while building the response of a `data:` URL.
25 #[error(transparent)]
26 Http(#[from] http::Error),
27 /// A header name given by the frontend is not a valid HTTP header name.
28 #[error(transparent)]
29 HttpInvalidHeaderName(#[from] http::header::InvalidHeaderName),
30 /// A header value is not a valid HTTP header value.
31 #[error(transparent)]
32 HttpInvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
33 /// URL not allowed by the scope.
34 ///
35 /// Raised for the URL requested by the frontend, and - when the
36 /// [`scope_redirects`](crate::Config::scope_redirects) configuration is enabled - for any
37 /// redirect target that is not allowed by the scope.
38 #[error("url not allowed on the configured scope: {0}")]
39 UrlNotAllowed(Url),
40 /// Failed to parse a URL.
41 #[error(transparent)]
42 UrlParseError(#[from] url::ParseError),
43 /// HTTP method error.
44 #[error(transparent)]
45 HttpMethod(#[from] http::method::InvalidMethod),
46 /// The requested URL uses an unsupported scheme. Only `http`, `https` and `data` are handled.
47 #[error("scheme {0} not supported")]
48 SchemeNotSupport(String),
49 /// The request was aborted by the frontend before the response was received.
50 #[error("Request canceled")]
51 RequestCanceled,
52 /// Error from the file system plugin.
53 #[error(transparent)]
54 FsError(#[from] tauri_plugin_fs::Error),
55 /// The `data:` URL could not be processed.
56 #[error("failed to process data url")]
57 DataUrlError,
58 /// The body of the `data:` URL could not be decoded into bytes.
59 #[error("failed to decode data url into bytes")]
60 DataUrlDecodeError,
61 /// Error from the Tauri APIs, raised for instance while resolving a path or while reading the
62 /// webview resource table.
63 #[error(transparent)]
64 Tauri(#[from] tauri::Error),
65 /// A response header value is not valid UTF-8, so it cannot be forwarded to the frontend.
66 #[error(transparent)]
67 Utf8(#[from] std::string::FromUtf8Error),
68 /// The frontend requested dangerous client settings, but the `dangerous-settings` Cargo
69 /// feature is not enabled.
70 #[error("dangerous settings used but are not enabled")]
71 DangerousSettings,
72}
73
74impl Serialize for Error {
75 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
76 where
77 S: Serializer,
78 {
79 serializer.serialize_str(self.to_string().as_ref())
80 }
81}
82
83/// Alias for a [`Result`](std::result::Result) with the error type [`Error`].
84pub type Result<T> = std::result::Result<T, Error>;