Files
dioxus/src/error.rs
T
Ben Sully ead183dd2c Add minimal proxy capabilities to dioxus serve
This adds an MVP of some proxying capabilities to the `dioxus serve`
server. The config is similar to that of `trunk serve`: the user can
specify one or more proxy backends under `[[web.proxy]]` in Dioxus.toml,
and the server will intercept requests targeted at the _path_ of that
configured backend and forward them to the backend server.

Example
-------

For example, if the dev server is serving on port 8080 with this config:

```
[[web.proxy]]
backend = "http://localhost:9000/api"
```

then requests to http://localhost:8080/api,
http://localhost:8080/api/ and http://localhost:8080/api/any-subpath
to be forwarded to the respective paths on http://localhost:9000.

This PR doesn't handle path rewriting or anything yet but it would be
fairly simple to add in future if anyone needs it.
2023-02-27 10:44:32 +00:00

81 lines
1.7 KiB
Rust

use thiserror::Error as ThisError;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(ThisError, Debug)]
pub enum Error {
/// Used when errors need to propogate but are too unique to be typed
#[error("{0}")]
Unique(String),
#[error("I/O Error: {0}")]
IO(#[from] std::io::Error),
#[error("Format Error: {0}")]
FormatError(#[from] std::fmt::Error),
#[error("Format failed: {0}")]
ParseError(String),
#[error("Runtime Error: {0}")]
RuntimeError(String),
#[error("Failed to write error")]
FailedToWrite,
#[error("Build Failed: {0}")]
BuildFailed(String),
#[error("Cargo Error: {0}")]
CargoError(String),
#[error("{0}")]
CustomError(String),
#[error("Invalid proxy URL: {0}")]
InvalidProxy(#[from] hyper::http::uri::InvalidUri),
#[error("Error proxying request: {0}")]
ProxyRequestError(hyper::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl From<&str> for Error {
fn from(s: &str) -> Self {
Error::Unique(s.to_string())
}
}
impl From<String> for Error {
fn from(s: String) -> Self {
Error::Unique(s)
}
}
impl From<html_parser::Error> for Error {
fn from(e: html_parser::Error) -> Self {
Self::ParseError(e.to_string())
}
}
impl From<hyper::Error> for Error {
fn from(e: hyper::Error) -> Self {
Self::RuntimeError(e.to_string())
}
}
#[macro_export]
macro_rules! custom_error {
($msg:literal $(,)?) => {
Err(Error::CustomError(format!($msg)))
};
($err:expr $(,)?) => {
Err(Error::from($err))
};
($fmt:expr, $($arg:tt)*) => {
Err(Error::CustomError(format!($fmt, $($arg)*)))
};
}