Skip to content

Commit d22a810

Browse files
authored
Merge pull request #38 from mkantor/better-bug-message
Improve error messages for Operator bugs.
2 parents 4f80ed9 + e2d177c commit d22a810

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

src/content/content_directory.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::Route;
2+
use crate::bug_message;
23
use std::env;
34
use std::fs::File;
45
use std::os::unix::fs::PermissionsExt;
@@ -210,10 +211,12 @@ impl ContentFile {
210211
route_string.push(Self::PATH_SEPARATOR);
211212
route_string.push_str(relative_path_without_extensions);
212213

213-
route_string.parse::<Route>().map_err(|error| ContentFileError(format!(
214-
"You've encountered a bug! This should never happen: Could not create route from path: {}",
215-
error
216-
)))
214+
route_string.parse::<Route>().map_err(|error| {
215+
ContentFileError(format!(
216+
bug_message!("This should never happen: Could not create route from path: {}"),
217+
error,
218+
))
219+
})
217220
}?;
218221

219222
Ok(ContentFile {

src/content/content_engine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::content_item::*;
44
use super::content_registry::*;
55
use super::handlebars_helpers::*;
66
use super::*;
7+
use crate::bug_message;
78
use handlebars::{self, Handlebars};
89
use mime_guess::MimeGuess;
910
use std::collections::hash_map::Entry;
@@ -53,7 +54,7 @@ pub enum ContentLoadingError {
5354
source: ContentIndexUpdateError,
5455
},
5556

56-
#[error("You've encountered a bug! This should never happen: {}", .0)]
57+
#[error("{} This should never happen: {}", bug_message!(), .0)]
5758
Bug(String),
5859
}
5960

src/content/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod mime;
99
mod route;
1010
mod test_lib;
1111

12+
use crate::bug_message;
1213
use bytes::Bytes;
1314
use content_index::ContentIndex;
1415
use content_item::RenderingFailedError;
@@ -58,7 +59,7 @@ pub enum RenderError {
5859
#[error("The requested content cannot be rendered as an acceptable media type.")]
5960
CannotProvideAcceptableMediaType,
6061

61-
#[error("You've encountered a bug! This should never happen: {}", .0)]
62+
#[error("{} This should never happen: {}", bug_message!(), .0)]
6263
Bug(String),
6364
}
6465

src/http.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,16 @@ where
9898
let media_range_from_url = MimeGuess::from_path(path).first();
9999
let path_without_extension = if media_range_from_url.is_some() {
100100
// Drop the extension from the path.
101-
path.rsplitn(2, '.').last().expect(
102-
"You've encountered a bug! This should never happen: Calling rsplitn(2, ..) on \
103-
a non-empty string returned an empty iterator. This should be impossible!",
104-
)
101+
path.rsplitn(2, '.').last().expect(bug_message!(
102+
"Calling rsplitn(2, ..) on a non-empty string returned an empty iterator. This should be impossible!",
103+
))
105104
} else {
106105
path
107106
};
108107

109108
match path_without_extension.parse::<Route>() {
110109
Err(error) => panic!(
111-
"You've encountered a bug! This should never happen: HTTP request path could not be parsed into a Route: {}",
110+
bug_message!("This should never happen: HTTP request path could not be parsed into a Route: {}"),
112111
error,
113112
),
114113
Ok(request_route) => {
@@ -266,9 +265,10 @@ where
266265
{
267266
let error_code = if !status_code.is_client_error() && !status_code.is_server_error() {
268267
log::error!(
269-
"You've encountered a bug! This should never happen: \
270-
The HTTP status code given to the error handler ({}) does not indicate an error.",
271-
status_code
268+
bug_message!(
269+
"This should never happen: The HTTP status code given to the error handler ({}) does not indicate an error.",
270+
),
271+
status_code,
272272
);
273273
http::StatusCode::INTERNAL_SERVER_ERROR
274274
} else {

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ pub struct ServerVersion(pub &'static str);
1212
pub struct ServerInfo {
1313
pub version: ServerVersion,
1414
}
15+
16+
#[macro_export]
17+
macro_rules! bug_message {
18+
() => {
19+
"You've encountered a bug in Operator! Please open an issue at <https://github.com/mkantor/operator/issues>."
20+
};
21+
($detail:expr$(,)?) => {
22+
concat!(bug_message!(), " ", $detail)
23+
};
24+
}

0 commit comments

Comments
 (0)