forked from rust-lang/docs.rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.rs
More file actions
248 lines (223 loc) · 7.94 KB
/
error.rs
File metadata and controls
248 lines (223 loc) · 7.94 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use crate::{
db::PoolError,
web::{page::WebPage, releases::Search, ErrorPage},
};
use failure::Fail;
use iron::{status::Status, Handler, IronError, IronResult, Request, Response};
use std::{error::Error, fmt};
#[derive(Debug, Copy, Clone)]
pub enum Nope {
ResourceNotFound,
BuildNotFound,
CrateNotFound,
VersionNotFound,
NoResults,
InternalServerError,
}
impl fmt::Display for Nope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Nope::ResourceNotFound => "Requested resource not found",
Nope::BuildNotFound => "Requested build not found",
Nope::CrateNotFound => "Requested crate not found",
Nope::VersionNotFound => "Requested crate does not have specified version",
Nope::NoResults => "Search yielded no results",
Nope::InternalServerError => "Internal server error",
})
}
}
impl Error for Nope {}
impl From<Nope> for IronError {
fn from(err: Nope) -> IronError {
use iron::status;
let status = match err {
Nope::ResourceNotFound
| Nope::BuildNotFound
| Nope::CrateNotFound
| Nope::VersionNotFound
| Nope::NoResults => status::NotFound,
Nope::InternalServerError => status::InternalServerError,
};
IronError::new(err, status)
}
}
impl Handler for Nope {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
match *self {
Nope::ResourceNotFound => {
// user tried to navigate to a resource (doc page/file) that doesn't exist
// TODO: Display the attempted page
ErrorPage {
title: "The requested resource does not exist",
message: Some("no such resource".into()),
status: Status::NotFound,
}
.into_response(req)
}
Nope::BuildNotFound => ErrorPage {
title: "The requested build does not exist",
message: Some("no such build".into()),
status: Status::NotFound,
}
.into_response(req),
Nope::CrateNotFound => {
// user tried to navigate to a crate that doesn't exist
// TODO: Display the attempted crate and a link to a search for said crate
ErrorPage {
title: "The requested crate does not exist",
message: Some("no such crate".into()),
status: Status::NotFound,
}
.into_response(req)
}
Nope::VersionNotFound => {
// user tried to navigate to a crate with a version that does not exist
// TODO: Display the attempted crate and version
ErrorPage {
title: "The requested version does not exist",
message: Some("no such version for this crate".into()),
status: Status::NotFound,
}
.into_response(req)
}
Nope::NoResults => {
let mut params = req.url.as_ref().query_pairs();
if let Some((_, query)) = params.find(|(key, _)| key == "query") {
// this used to be a search
Search {
title: format!("No crates found matching '{}'", query),
search_query: Some(query.into_owned()),
status: Status::NotFound,
..Default::default()
}
.into_response(req)
} else {
// user did a search with no search terms
Search {
title: "No results given for empty search query".to_owned(),
status: Status::NotFound,
..Default::default()
}
.into_response(req)
}
}
Nope::InternalServerError => {
// something went wrong, details should have been logged
ErrorPage {
title: "Internal server error",
message: Some("internal server error".into()),
status: Status::InternalServerError,
}
.into_response(req)
}
}
}
}
impl From<PoolError> for IronError {
fn from(err: PoolError) -> IronError {
IronError::new(err.compat(), Status::InternalServerError)
}
}
#[cfg(test)]
mod tests {
use crate::test::wrapper;
use kuchiki::traits::TendrilSink;
#[test]
fn check_404_page_content_crate() {
wrapper(|env| {
let page = kuchiki::parse_html().one(
env.frontend()
.get("/crate-which-doesnt-exist")
.send()?
.text()?,
);
assert_eq!(page.select("#crate-title").unwrap().count(), 1);
assert_eq!(
page.select("#crate-title")
.unwrap()
.next()
.unwrap()
.text_contents(),
"The requested crate does not exist",
);
Ok(())
});
}
#[test]
fn check_404_page_content_resource() {
wrapper(|env| {
let page = kuchiki::parse_html().one(
env.frontend()
.get("/resource-which-doesnt-exist.js")
.send()?
.text()?,
);
assert_eq!(page.select("#crate-title").unwrap().count(), 1);
assert_eq!(
page.select("#crate-title")
.unwrap()
.next()
.unwrap()
.text_contents(),
"The requested resource does not exist",
);
Ok(())
});
}
#[test]
fn check_404_page_content_not_semver_version() {
wrapper(|env| {
env.fake_release().name("dummy").create()?;
let page =
kuchiki::parse_html().one(env.frontend().get("/dummy/not-semver").send()?.text()?);
assert_eq!(page.select("#crate-title").unwrap().count(), 1);
assert_eq!(
page.select("#crate-title")
.unwrap()
.next()
.unwrap()
.text_contents(),
"The requested version does not exist",
);
Ok(())
});
}
#[test]
fn check_404_page_content_nonexistent_version() {
wrapper(|env| {
env.fake_release().name("dummy").version("1.0.0").create()?;
let page = kuchiki::parse_html().one(env.frontend().get("/dummy/2.0").send()?.text()?);
assert_eq!(page.select("#crate-title").unwrap().count(), 1);
assert_eq!(
page.select("#crate-title")
.unwrap()
.next()
.unwrap()
.text_contents(),
"The requested version does not exist",
);
Ok(())
});
}
#[test]
fn check_404_page_content_any_version_all_yanked() {
wrapper(|env| {
env.fake_release()
.name("dummy")
.version("1.0.0")
.yanked(true)
.create()?;
let page = kuchiki::parse_html().one(env.frontend().get("/dummy/*").send()?.text()?);
assert_eq!(page.select("#crate-title").unwrap().count(), 1);
assert_eq!(
page.select("#crate-title")
.unwrap()
.next()
.unwrap()
.text_contents(),
"The requested version does not exist",
);
Ok(())
});
}
}