Skip to content

Commit bfef8df

Browse files
jyn514Joshua Nelson
authored andcommitted
Add impl From<Nope> for IronError and use it across the codebase
This cleans things up quite a bit. It also makes the eventual transition to hyper easier, since we can modify the `From` impl instead of changing each line one at a time.
1 parent b2b7458 commit bfef8df

File tree

8 files changed

+50
-49
lines changed

8 files changed

+50
-49
lines changed

src/web/crate_details.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{match_version, redirect_base, render_markdown, MatchSemver, MetaData
22
use crate::{db::Pool, impl_webpage, web::page::WebPage};
33
use chrono::{DateTime, NaiveDateTime, Utc};
44
use iron::prelude::*;
5-
use iron::{status, Url};
5+
use iron::Url;
66
use postgres::Client;
77
use router::Router;
88
use serde::{ser::Serializer, Serialize};
@@ -295,14 +295,14 @@ pub fn crate_details_handler(req: &mut Request) -> IronResult<Response> {
295295

296296
let mut conn = extension!(req, Pool).get()?;
297297

298-
match match_version(&mut conn, &name, req_version).and_then(|m| m.assume_exact()) {
299-
Ok(MatchSemver::Exact((version, _))) => {
298+
match match_version(&mut conn, &name, req_version).and_then(|m| m.assume_exact())? {
299+
MatchSemver::Exact((version, _)) => {
300300
let details = cexpect!(req, CrateDetails::new(&mut conn, &name, &version));
301301

302302
CrateDetailsPage { details }.into_response(req)
303303
}
304304

305-
Ok(MatchSemver::Semver((version, _))) => {
305+
MatchSemver::Semver((version, _)) => {
306306
let url = ctry!(
307307
req,
308308
Url::parse(&format!(
@@ -315,8 +315,6 @@ pub fn crate_details_handler(req: &mut Request) -> IronResult<Response> {
315315

316316
Ok(super::redirect(url))
317317
}
318-
319-
Err(err) => Err(IronError::new(err, status::NotFound)),
320318
}
321319
}
322320

src/web/error.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ impl fmt::Display for Nope {
2929

3030
impl Error for Nope {}
3131

32+
impl From<Nope> for IronError {
33+
fn from(err: Nope) -> IronError {
34+
use iron::status;
35+
36+
let status = match err {
37+
Nope::ResourceNotFound
38+
| Nope::CrateNotFound
39+
| Nope::VersionNotFound
40+
| Nope::NoResults => status::NotFound,
41+
Nope::InternalServerError => status::InternalServerError,
42+
};
43+
44+
IronError::new(err, status)
45+
}
46+
}
47+
3248
impl Handler for Nope {
3349
fn handle(&self, req: &mut Request) -> IronResult<Response> {
3450
match *self {

src/web/file.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::storage::{Blob, Storage};
44
use crate::{error::Result, Config};
5-
use iron::{status, Handler, IronError, IronResult, Request, Response};
5+
use iron::{status, Handler, IronResult, Request, Response};
66

77
#[derive(Debug)]
88
pub(crate) struct File(pub(crate) Blob);
@@ -62,10 +62,7 @@ impl Handler for DatabaseFileHandler {
6262
if let Ok(file) = File::from_path(&storage, &path, &config) {
6363
Ok(file.serve())
6464
} else {
65-
Err(IronError::new(
66-
super::error::Nope::CrateNotFound,
67-
status::NotFound,
68-
))
65+
Err(super::error::Nope::CrateNotFound.into())
6966
}
7067
}
7168
}

src/web/releases.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use iron::{
1212
headers::{ContentType, Expires, HttpDate},
1313
mime::{Mime, SubLevel, TopLevel},
1414
modifiers::Redirect,
15-
status, IronError, IronResult, Request, Response, Url,
15+
status, IronResult, Request, Response, Url,
1616
};
1717
use postgres::Client;
1818
use router::Router;
@@ -420,7 +420,7 @@ pub fn author_handler(req: &mut Request) -> IronResult<Response> {
420420
let author = router
421421
.find("author")
422422
// TODO: Accurate error here, the author wasn't provided
423-
.ok_or_else(|| IronError::new(Nope::CrateNotFound, status::NotFound))?;
423+
.ok_or(Nope::CrateNotFound)?;
424424

425425
let (author_name, releases) = {
426426
let mut conn = extension!(req, Pool).get()?;
@@ -442,7 +442,7 @@ pub fn author_handler(req: &mut Request) -> IronResult<Response> {
442442

443443
if releases.is_empty() {
444444
// TODO: Accurate error here, the author wasn't found
445-
return Err(IronError::new(Nope::CrateNotFound, status::NotFound));
445+
return Err(Nope::CrateNotFound.into());
446446
}
447447

448448
// Show next and previous page buttons
@@ -621,7 +621,7 @@ pub fn search_handler(req: &mut Request) -> IronResult<Response> {
621621
}
622622
.into_response(req)
623623
} else {
624-
Err(IronError::new(Nope::NoResults, status::NotFound))
624+
Err(Nope::NoResults.into())
625625
}
626626
}
627627

src/web/routes.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,7 @@ impl Handler for BlockBlacklistedPrefixes {
284284
fn handle(&self, req: &mut iron::Request) -> iron::IronResult<iron::Response> {
285285
if let Some(prefix) = req.url.path().get(0) {
286286
if self.blacklist.contains(*prefix) {
287-
return Err(iron::IronError::new(
288-
super::error::Nope::CrateNotFound,
289-
iron::status::NotFound,
290-
));
287+
return Err(super::error::Nope::CrateNotFound.into());
291288
}
292289
}
293290
self.handler.handle(req)

src/web/rustdoc.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
use iron::{
1313
headers::{CacheControl, CacheDirective, Expires, HttpDate},
1414
modifiers::Redirect,
15-
status, Handler, IronError, IronResult, Request, Response, Url,
15+
status, Handler, IronResult, Request, Response, Url,
1616
};
1717
use lol_html::errors::RewritingError;
1818
use router::Router;
@@ -110,10 +110,10 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
110110

111111
let path = req.url.path();
112112
let path = path.join("/");
113-
match File::from_path(&storage, &path, &config) {
114-
Ok(f) => return Ok(f.serve()),
115-
Err(..) => return Err(IronError::new(Nope::ResourceNotFound, status::NotFound)),
116-
}
113+
return match File::from_path(&storage, &path, &config) {
114+
Ok(f) => Ok(f.serve()),
115+
Err(..) => Err(Nope::ResourceNotFound.into()),
116+
};
117117
}
118118
} else if req
119119
.url
@@ -142,19 +142,13 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
142142

143143
// it doesn't matter if the version that was given was exact or not, since we're redirecting
144144
// anyway
145-
let (version, id) = match match_version(&mut conn, &crate_name, req_version) {
146-
Ok(v) => {
147-
if let Some(new_name) = v.corrected_name {
148-
// `match_version` checked against -/_ typos, so if we have a name here we should
149-
// use that instead
150-
crate_name = new_name;
151-
}
152-
v.version.into_parts()
153-
}
154-
Err(err) => {
155-
return Err(IronError::new(err, status::NotFound));
156-
}
157-
};
145+
let v = match_version(&mut conn, &crate_name, req_version)?;
146+
if let Some(new_name) = v.corrected_name {
147+
// `match_version` checked against -/_ typos, so if we have a name here we should
148+
// use that instead
149+
crate_name = new_name;
150+
}
151+
let (version, id) = v.version.into_parts();
158152

159153
// get target name and whether it has docs
160154
// FIXME: This is a bit inefficient but allowing us to use less code in general
@@ -288,8 +282,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
288282
// * If both the name and the version are an exact match, return the version of the crate.
289283
// * If there is an exact match, but the requested crate name was corrected (dashes vs. underscores), redirect to the corrected name.
290284
// * If there is a semver (but not exact) match, redirect to the exact version.
291-
let release_found = match_version(&mut conn, &name, url_version)
292-
.map_err(|err| IronError::new(err, status::NotFound))?;
285+
let release_found = match_version(&mut conn, &name, url_version)?;
293286

294287
let version = match release_found.version {
295288
MatchSemver::Exact((version, _)) => {
@@ -349,7 +342,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
349342
return if ctry!(req, storage.exists(&path)) {
350343
redirect(&name, &version, &req_path[3..])
351344
} else {
352-
Err(IronError::new(Nope::ResourceNotFound, status::NotFound))
345+
Err(Nope::ResourceNotFound.into())
353346
};
354347
}
355348
};
@@ -489,7 +482,7 @@ pub fn target_redirect_handler(req: &mut Request) -> IronResult<Response> {
489482

490483
let crate_details = match CrateDetails::new(&mut conn, &name, &version) {
491484
Some(krate) => krate,
492-
None => return Err(IronError::new(Nope::ResourceNotFound, status::NotFound)),
485+
None => return Err(Nope::ResourceNotFound.into()),
493486
};
494487

495488
// [crate, :name, :version, target-redirect, :target, *path]
@@ -623,7 +616,7 @@ impl Handler for SharedResourceHandler {
623616
}
624617

625618
// Just always return a 404 here - the main handler will then try the other handlers
626-
Err(IronError::new(Nope::ResourceNotFound, status::NotFound))
619+
Err(Nope::ResourceNotFound.into())
627620
}
628621
}
629622

src/web/source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
web::{error::Nope, file::File as DbFile, page::WebPage, MetaData},
77
Config, Storage,
88
};
9-
use iron::{status::Status, IronError, IronResult, Request, Response};
9+
use iron::{IronResult, Request, Response};
1010
use postgres::Client;
1111
use router::Router;
1212
use serde::Serialize;
@@ -213,8 +213,8 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
213213
(None, false)
214214
};
215215

216-
let file_list = FileList::from_path(&mut conn, &name, &version, &req_path)
217-
.ok_or_else(|| IronError::new(Nope::NoResults, Status::NotFound))?;
216+
let file_list =
217+
FileList::from_path(&mut conn, &name, &version, &req_path).ok_or(Nope::NoResults)?;
218218

219219
SourcePage {
220220
file_list,

src/web/statics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use iron::{
44
headers::CacheDirective,
55
headers::{CacheControl, ContentLength, ContentType, LastModified},
66
status::Status,
7-
IronError, IronResult, Request, Response, Url,
7+
IronResult, Request, Response, Url,
88
};
99
use mime_guess::MimeGuess;
1010
use router::Router;
@@ -39,15 +39,15 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
3939
fn serve_file(req: &Request, file: &str) -> IronResult<Response> {
4040
// Filter out files that attempt to traverse directories
4141
if file.contains("..") || file.contains('/') || file.contains('\\') {
42-
return Err(IronError::new(Nope::ResourceNotFound, Status::NotFound));
42+
return Err(Nope::ResourceNotFound.into());
4343
}
4444

4545
// Find the first path that actually exists
4646
let path = STATIC_SEARCH_PATHS
4747
.iter()
4848
.map(|p| Path::new(p).join(file))
4949
.find(|p| p.exists())
50-
.ok_or_else(|| IronError::new(Nope::ResourceNotFound, Status::NotFound))?;
50+
.ok_or(Nope::ResourceNotFound)?;
5151
let contents = ctry!(req, fs::read(&path));
5252

5353
// If we can detect the file's mime type, set it
@@ -106,7 +106,7 @@ pub(super) fn ico_handler(req: &mut Request) -> IronResult<Response> {
106106
if let Some(&"favicon.ico") = req.url.path().last() {
107107
// if we're looking for exactly "favicon.ico", we need to defer to the handler that loads
108108
// from `public_html`, so return a 404 here to make the main handler carry on
109-
Err(IronError::new(Nope::ResourceNotFound, Status::NotFound))
109+
Err(Nope::ResourceNotFound.into())
110110
} else {
111111
// if we're looking for something like "favicon-20190317-1.35.0-nightly-c82834e2b.ico",
112112
// redirect to the plain one so that the above branch can trigger with the correct filename

0 commit comments

Comments
 (0)