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
use axum::http::{ StatusCode };
use axum::response::{ IntoResponse, Response };

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

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

pub(crate) enum Error {
	FromConfigCrate(config::ConfigError),
	FromMailSendCrate(mail_send::Error),
	FromSerdeJsonCrate(serde_json::Error),
	FromStdIo(io::Error),
	FromTeraCrate(tera::Error),
	InvalidPort(num::TryFromIntError),
	ListNotFound(String),
	MissingContactEmail,
	MissingFormField(String),
	PageNotFound(String),
	PostNotFound(String),
	PathContainsInvalidUtf8(PathBuf),
}

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

		match *self {
			FromConfigCrate(ref e) => write!(f, "Error from the config crate: {}", e),
			FromMailSendCrate(ref e) => write!(f, "Error from the mail_send crate: {}", e),
			FromSerdeJsonCrate(ref e) => write!(f, "Error from the serde_json crate: {}", e),
			FromStdIo(ref e) => write!(f, "IO error: {}", e),
			FromTeraCrate(ref e) => write!(f, "Error from the tera crate: {}", e),
			InvalidPort(ref e) => write!(f, "Invalid network.port: {}", e),
			MissingContactEmail => write!(f, "Missing contact email"),
			ListNotFound(ref slug) => write!(f, "List not found: {}", slug),
			MissingFormField(ref field) => write!(f, "Missing form field: {}", field),
			PageNotFound(ref slug) => write!(f, "Page not found: {}", slug),
			PostNotFound(ref slug) => write!(f, "Post not found: {}", slug),
			PathContainsInvalidUtf8(ref pathbuf) => write!(f, "Path contains invalid UTF-8: {}", pathbuf.display()),
		}
	}
}

impl From<config::ConfigError> for Error {
	fn from(e: config::ConfigError) -> Error {
		tracing::debug!("Error::FromConfigCrate: {:?}", e);
		Error::FromConfigCrate(e)
	}
}

impl From<mail_send::Error> for Error {
	fn from(e: mail_send::Error) -> Error {
		tracing::debug!("Error::FromMailSendCrate: {:?}", e);
		Error::FromMailSendCrate(e)
	}
}

impl From<serde_json::Error> for Error {
	fn from(e: serde_json::Error) -> Error {
		tracing::debug!("Error::FromSerdeJsonCrate: {:?}", e);
		Error::FromSerdeJsonCrate(e)
	}
}

impl From<io::Error> for Error {
	fn from(e: io::Error) -> Error {
		tracing::debug!("Error::FromStdIo: {:?}", e);
		Error::FromStdIo(e)
	}
}

impl From<tera::Error> for Error {
	fn from(e: tera::Error) -> Error {
		tracing::debug!("Error::FromTeraCrate: {:?}", e);
		Error::FromTeraCrate(e)
	}
}

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

		tracing::error!("Building response for error \"{}\"", self);

		let (http_status_code, message) = match self {
			ListNotFound(..) => (StatusCode::NOT_FOUND, "List not found"),
			MissingFormField(..) => (StatusCode::BAD_REQUEST, "Missing form field"),
			PageNotFound(..) => (StatusCode::NOT_FOUND, "Page not found"),
			PostNotFound(..) => (StatusCode::NOT_FOUND, "Post not found"),
			_ => (StatusCode::INTERNAL_SERVER_ERROR, "The server encountered an unexpected error"),
		};

		(http_status_code, message).into_response()
	}
}