aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-06-24 08:21:27 +0200
committerMartin Fischer <martin@push-f.com>2021-06-24 08:21:29 +0200
commitb0cd90369a57187c35b2835b665645ba060e3ec7 (patch)
tree3fc2b6837fe36ae17b9b0246aad2367db8c8d0f7 /src/error.rs
parentafd6c8c083bcc11523cd4143e408e0831613fed9 (diff)
refactor: move Error enum and impls to own module
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..c006255
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,49 @@
+use sputnik::hyper_body::FormError;
+use sputnik::request::QueryError;
+use std::str::Utf8Error;
+
+use crate::Context;
+
+pub enum Error {
+ /// A 400 bad request error.
+ BadRequest(String),
+ /// A 401 unauthorized error.
+ Unauthorized(String, Context),
+ /// A 403 forbidden error.
+ Forbidden(String),
+ /// A 404 not found error.
+ NotFound(String),
+ /// A 500 internal server error.
+ Internal(String),
+ /// A 302 redirect to the given path.
+ Redirect(String),
+
+ // TODO: use Redirect instead
+ /// Missing trailing slash.
+ MissingTrailingSlash(String),
+}
+
+impl From<Utf8Error> for Error {
+ fn from(_: Utf8Error) -> Self {
+ Self::BadRequest("invalid UTF-8".into())
+ }
+}
+
+impl From<git2::Error> for Error {
+ fn from(e: git2::Error) -> Self {
+ eprintln!("git error: {}", e);
+ Self::Internal("something went wrong with git".into())
+ }
+}
+
+impl From<FormError> for Error {
+ fn from(e: FormError) -> Self {
+ Self::BadRequest(e.to_string())
+ }
+}
+
+impl From<QueryError> for Error {
+ fn from(e: QueryError) -> Self {
+ Self::BadRequest(e.to_string())
+ }
+}