Echo Writes Code

errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use axum::http::{ StatusCode };
use axum::response::{ IntoResponse, Response };

use std::io;
use std::fmt;
use std::num;
use std::path::{ PathBuf };
use std::string;

pub(crate) type Result<T> = ::std::result::Result<T, Error>;

#[derive(Debug)]
pub(crate) enum Error {
	CannotDetermineRepositoryName(PathBuf),
	FromConfigCrate(config::ConfigError),
	FromGit2Crate(git2::Error),
	FromRegexCrate(regex::Error),
	FromStdIo(io::Error),
	FromTeraCrate(tera::Error),
	InvalidBlobPath(String),
	InvalidGitRoot(io::Error),
	InvalidPort(num::TryFromIntError),
	InvalidTreePath(String),
	NonUtf8Blob(string::FromUtf8Error),
	NonUtf8BranchName(string::FromUtf8Error),
	NonUtf8CommitMessage(string::FromUtf8Error),
	NonUtf8ObjectId(string::FromUtf8Error),
	NonUtf8TagName(string::FromUtf8Error),
	ObjectNotFoundByPath(String),
	RepositoryNotFound(String),
	UnexpectedObjectType(git2::ObjectType),
	UnnamedTreeEntry(git2::Oid),
	UntypedTreeEntry(git2::Oid),
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		use Error::*;

		match *self {
			CannotDetermineRepositoryName(ref path) => write!(f, "Cannot determine repository name for {}", path.display()),
			FromConfigCrate(ref e) => write!(f, "Error from the config crate: {}", e),
			FromGit2Crate(ref e) => write!(f, "Error from the git2 crate: {}", e),
			FromRegexCrate(ref e) => write!(f, "Error from the regex crate: {}", e),
			FromStdIo(ref e) => write!(f, "IO error: {}", e),
			FromTeraCrate(ref e) => write!(f, "Error from the tera crate: {}", e),
			InvalidBlobPath(ref p) => write!(f, "Invalid blob path: {}", p),
			InvalidGitRoot(ref e) => write!(f, "Invalid git.root: {}", e),
			InvalidPort(ref e) => write!(f, "Invalid network.port: {}", e),
			InvalidTreePath(ref p) => write!(f, "Invalid tree path: {}", p),
			NonUtf8Blob(ref e) => write!(f, "Blob is not valid UTF-8: {}", e),
			NonUtf8BranchName(ref e) => write!(f, "Branch name is not valid UTF-8: {}", e),
			NonUtf8CommitMessage(ref e) => write!(f, "Commit message is not valid UTF-8: {}", e),
			NonUtf8ObjectId(ref e) => write!(f, "Object ID is not valid UTF-8: {}", e),
			NonUtf8TagName(ref e) => write!(f, "Tag name is not valid UTF-8: {}", e),
			ObjectNotFoundByPath(ref p) => write!(f, "Object not found by path: {}", p),
			RepositoryNotFound(ref name) => write!(f, "Repository not found: {}", name),
			UnexpectedObjectType(ref t) => write!(f, "Unexpected object type: {}", t),
			UnnamedTreeEntry(ref oid) => write!(f, "Unnamed tree entry: {}", oid),
			UntypedTreeEntry(ref oid) => write!(f, "Untyped tree entry: {}", oid),
		}
	}
}

impl From<config::ConfigError> for Error {
	fn from(e: config::ConfigError) -> Error {
		Error::FromConfigCrate(e)
	}
}

impl From<git2::Error> for Error {
	fn from(e: git2::Error) -> Error {
		Error::FromGit2Crate(e)
	}
}

impl From<regex::Error> for Error {
	fn from(e: regex::Error) -> Error {
		Error::FromRegexCrate(e)
	}
}

impl From<io::Error> for Error {
	fn from(e: io::Error) -> Error {
		Error::FromStdIo(e)
	}
}

impl From<tera::Error> for Error {
	fn from(e: tera::Error) -> Error {
		Error::FromTeraCrate(e)
	}
}

impl IntoResponse for Error {
	fn into_response(self) -> Response {
		use Error::*;

		tracing::error!("{}", &self);

		let (status, message) = match self {
			InvalidBlobPath(..) => (StatusCode::BAD_REQUEST, "Invalid blob path"),
			InvalidTreePath(..) => (StatusCode::BAD_REQUEST, "Invalid tree path"),
			NonUtf8Blob(..) => (StatusCode::UNSUPPORTED_MEDIA_TYPE, "The requested blob's content is not valid UTF-8"),
			NonUtf8BranchName(..) => (StatusCode::UNSUPPORTED_MEDIA_TYPE, "The requested branch's name is not valid UTF-8"),
			NonUtf8CommitMessage(..) => (StatusCode::UNSUPPORTED_MEDIA_TYPE, "The requested commit's message is not valid UTF-8"),
			NonUtf8ObjectId(..) => (StatusCode::UNSUPPORTED_MEDIA_TYPE, "The requested object's ID is not valid UTF-8"),
			NonUtf8TagName(..) => (StatusCode::UNSUPPORTED_MEDIA_TYPE, "The requested tag's name is not valid UTF-8"),
			ObjectNotFoundByPath(..) => (StatusCode::NOT_FOUND, "Object not found"),
			RepositoryNotFound(..) => (StatusCode::NOT_FOUND, "Repository not found"),
			_ => (StatusCode::INTERNAL_SERVER_ERROR, "The server encountered an unexpected error")
		};

		(status, message).into_response()
	}
}