Repositories

grarr

(mirrored on github)

Wim Looman <wim@nemo157.com>
6ebe40 Use boxed errors
Wim Looman committed at 2016-03-14 17:21:58

Modified src/error.rs

@@ -1,60 +1,43 @@
use std::error;
use std::fmt;
use std::io;
use std::borrow::{ Cow, Borrow };
use git2;
#[derive(Debug)]
pub enum Error {
MissingExtension,
MissingPathComponent,
String(Cow<'static, str>),
Git(git2::Error),
Io(io::Error),
}
pub struct Error(Box<error::Error + Send + Sync>);
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::MissingExtension => "Missing request extension",
Error::MissingPathComponent => "Missing path component",
Error::String(ref s) => s.borrow(),
Error::Git(ref e) => e.description(),
Error::Io(ref e) => e.description(),
}
self.0.description()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Git(ref e) => e.fmt(f),
Error::Io(ref e) => e.fmt(f),
_ => f.write_str(error::Error::description(self)),
}
self.0.fmt(f)
}
}
impl From<git2::Error> for Error {
fn from(e: git2::Error) -> Error {
Error::Git(e)
Error(e.into())
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::Io(e)
Error(e.into())
}
}
impl From<&'static str> for Error {
fn from(s: &'static str) -> Error {
Error::String(s.into())
Error(s.into())
}
}
impl From<String> for Error {
fn from(s: String) -> Error {
Error::String(s.into())
Error(s.into())
}
}

Modified src/handler/blob.rs

@@ -8,8 +8,8 @@ pub struct Blob;
impl Handler for Blob {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::from("missing extension")), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let path = router.find("path").unwrap_or("");
let entry = try!(tree_entry::get_tree_entry(&context, path));
let referenced_commit = itry!(context.referenced_commit(), status::NotFound);

Modified src/handler/commit.rs

@@ -5,7 +5,7 @@ pub struct Commit;
impl Handler for Commit {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let commit = itry!(context.commit(), status::NotFound);
Html {
render: RepositoryWrapper(&context, &render::Commit(&context, &commit)),

Modified src/handler/commits.rs

@@ -8,7 +8,7 @@ pub struct Commits;
impl Handler for Commits {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let start_commit = req.url.clone().into_generic_url()
.query_pairs()
.unwrap_or_default()

Modified src/handler/compare.rs

@@ -7,7 +7,7 @@ pub struct Compare;
impl Handler for Compare {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let new_commit = itry!(context.referenced_commit(), status::NotFound);
// TODO: Read which old commit from url

Modified src/handler/git_smart_http/refs.rs

@@ -37,7 +37,7 @@ fn find_service(req: &Request) -> Option<String> {
impl Handler for Refs {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
match find_service(req).as_ref().map(|s| &**s) {
Some("git-upload-pack") => {
let head = itry!(context.repository.head());

Modified src/handler/pages.rs

@@ -44,11 +44,11 @@ fn get_markdown_response(context: &RepositoryContext, path: &str) -> Result<Resp
impl Handler for Pages {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
{
let mut context = itry!(req.extensions.get_mut::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let mut context = itry!(req.extensions.get_mut::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
context.reference = Some("gh-pages".to_owned());
}
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::from("missing extension")), status::InternalServerError);
let mut path = router.find("path").unwrap_or("").to_owned();
if path == "" || path.ends_with('/') {

Modified src/handler/repository.rs

@@ -5,7 +5,7 @@ pub struct Repository;
impl Handler for Repository {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let head_ref = itry!(context.repository.head(), status::InternalServerError);
let resolved_head = itry!(head_ref.resolve(), status::InternalServerError);
let head_id = itry!(resolved_head.target().ok_or(Error::from("Couldn't resolve head")), status::InternalServerError);

Modified src/handler/review.rs

@@ -8,9 +8,9 @@ pub struct Review;
impl Handler for Review {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let commit = itry!(router.find("ref").ok_or(Error::MissingPathComponent), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::from("missing extension")), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let commit = itry!(router.find("ref").ok_or(Error::from("missing path component")), status::InternalServerError);
let id = itry!(Oid::from_str(commit), status::BadRequest);
let review = itry!(context.repository.review_for(id), status::NotFound);
let root = format!("/{}", context.path);

Modified src/handler/reviews.rs

@@ -6,7 +6,7 @@ pub struct Reviews;
impl Handler for Reviews {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let mut reviews: Vec<_> = context.repository.all_reviews().and_then(|revs| revs.collect()).ok().unwrap_or_default();
reviews.sort_by(|a, b| a.request().timestamp().cmp(&b.request().timestamp()));
reviews.reverse();

Modified src/handler/settings.rs

@@ -10,7 +10,7 @@ pub struct SettingsPost;
impl Handler for Settings {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let settings = itry!(req.extensions.get::<settings::Settings>().ok_or(Error::MissingExtension), status::InternalServerError);
let settings = itry!(req.extensions.get::<settings::Settings>().ok_or(Error::from("missing extension")), status::InternalServerError);
Html {
render: &render::Settings(settings),
etag: None,
@@ -33,7 +33,7 @@ impl Handler for SettingsPost {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let settings = {
let map: Map = itry!(req.get::<Params>(), status::InternalServerError);
let settings = itry!(req.extensions.get::<settings::Settings>().ok_or(Error::MissingExtension), status::InternalServerError);
let settings = itry!(req.extensions.get::<settings::Settings>().ok_or(Error::from("missing extension")), status::InternalServerError);
settings.with(
map.iter().filter_map(|(key, value)| match *value {
Value::String(ref value) => Some((&**key, &**value)),

Modified src/handler/statics.rs

@@ -49,8 +49,8 @@ impl Static {
impl Handler for Static {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let path = Path::new(itry!(router.find("path").ok_or(Error::MissingPathComponent), status::InternalServerError));
let router = itry!(req.extensions.get::<Router>().ok_or(Error::from("missing extension")), status::InternalServerError);
let path = Path::new(itry!(router.find("path").ok_or(Error::from("missing path component")), status::InternalServerError));
let File(mime, entity_tag, buffer) = itry!(self.find_file(path).ok_or(Error::from("Static file not found")), status::NotFound);
let cache_headers = utils::cache_headers_for(&entity_tag, Duration::from_secs(86400));
if req.cache_matches(&entity_tag) {

Modified src/handler/tree.rs

@@ -8,8 +8,8 @@ pub struct Tree;
impl Handler for Tree {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::from("missing extension")), status::InternalServerError);
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::from("missing extension")), status::InternalServerError);
let entry = try!(tree_entry::get_tree_entry(&context, router.find("path").unwrap_or("")));
match entry.entry.kind() {
Some(git2::ObjectType::Tree) => {

Modified src/repository_context.rs

@@ -66,10 +66,10 @@ pub struct RepositoryContextHandler<H: Handler> {
impl<H: Handler> Handler for RepositoryContextHandler<H> {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let (path, reference) = {
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::from("missing extension")), status::InternalServerError);
(router.find("repo").map(ToOwned::to_owned), router.find("ref").map(ToOwned::to_owned))
};
let path = itry!(path.ok_or(Error::MissingPathComponent), status::InternalServerError);
let path = itry!(path.ok_or(Error::from("missing path component")), status::InternalServerError);
let full_path = self.canonical_root.join(&path);
let full_canonical_path = itry!(fs::canonicalize(&full_path), status::NotFound);
if full_path == full_canonical_path {