Turn panics into errors in ServeSession

This commit is contained in:
Lucien Greathouse
2020-03-26 12:16:55 -07:00
parent 03c297190d
commit 729a7f0053
3 changed files with 45 additions and 20 deletions

View File

@@ -19,13 +19,13 @@ pub struct ProjectError(#[from] Error);
#[derive(Debug, Error)] #[derive(Debug, Error)]
enum Error { enum Error {
#[error("Rojo project I/O error")] #[error(transparent)]
Io { Io {
#[from] #[from]
source: io::Error, source: io::Error,
}, },
#[error("Error parsing Rojo project")] #[error("Error parsing Rojo project in path {}", .path.display())]
Json { Json {
source: serde_json::Error, source: serde_json::Error,
path: PathBuf, path: PathBuf,

View File

@@ -1,6 +1,6 @@
use std::{ use std::{
collections::HashSet, collections::HashSet,
path::Path, path::{Path, PathBuf},
sync::{Arc, Mutex, MutexGuard}, sync::{Arc, Mutex, MutexGuard},
time::Instant, time::Instant,
}; };
@@ -13,13 +13,13 @@ use thiserror::Error;
use crate::{ use crate::{
change_processor::ChangeProcessor, change_processor::ChangeProcessor,
message_queue::MessageQueue, message_queue::MessageQueue,
project::Project, project::{Project, ProjectError},
session_id::SessionId, session_id::SessionId,
snapshot::{ snapshot::{
apply_patch_set, compute_patch_set, AppliedPatchSet, InstanceContext, apply_patch_set, compute_patch_set, AppliedPatchSet, InstanceContext,
InstancePropertiesWithMeta, PatchSet, PathIgnoreRule, RojoTree, InstancePropertiesWithMeta, PatchSet, PathIgnoreRule, RojoTree,
}, },
snapshot_middleware::snapshot_from_vfs, snapshot_middleware::{snapshot_from_vfs, SnapshotError},
}; };
/// Contains all of the state for a Rojo serve session. /// Contains all of the state for a Rojo serve session.
@@ -97,10 +97,11 @@ impl ServeSession {
log::trace!("Starting new ServeSession at path {}", start_path.display()); log::trace!("Starting new ServeSession at path {}", start_path.display());
log::trace!("Loading project file from {}", start_path.display()); log::debug!("Loading project file from {}", start_path.display());
let root_project = Project::load_fuzzy(start_path) let root_project =
.expect("TODO: Project load failed") Project::load_fuzzy(start_path)?.ok_or_else(|| ServeSessionError::NoProjectFound {
.expect("TODO: No project was found."); path: start_path.to_owned(),
})?;
let mut tree = RojoTree::new(InstancePropertiesWithMeta { let mut tree = RojoTree::new(InstancePropertiesWithMeta {
properties: RbxInstanceProperties { properties: RbxInstanceProperties {
@@ -126,8 +127,7 @@ impl ServeSession {
instance_context.add_path_ignore_rules(rules); instance_context.add_path_ignore_rules(rules);
log::trace!("Generating snapshot of instances from VFS"); log::trace!("Generating snapshot of instances from VFS");
let snapshot = snapshot_from_vfs(&instance_context, &vfs, &start_path) let snapshot = snapshot_from_vfs(&instance_context, &vfs, &start_path)?
.expect("snapshot failed")
.expect("snapshot did not return an instance"); .expect("snapshot did not return an instance");
log::trace!("Computing initial patch set"); log::trace!("Computing initial patch set");
@@ -208,4 +208,23 @@ impl ServeSession {
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ServeSessionError {} pub enum ServeSessionError {
#[error(
"Rojo requires a project file, but no project file was found in path {}\n\
See https://rojo.space/docs/ for guides and documentation.",
.path.display()
)]
NoProjectFound { path: PathBuf },
#[error(transparent)]
Project {
#[from]
source: ProjectError,
},
#[error(transparent)]
Snapshot {
#[from]
source: SnapshotError,
},
}

View File

@@ -17,20 +17,26 @@ mod rbxmx;
mod txt; mod txt;
mod util; mod util;
pub use self::error::*;
use std::path::Path; use std::path::Path;
use memofs::Vfs; use memofs::Vfs;
use self::middleware::{SnapshotInstanceResult, SnapshotMiddleware};
use self::{
csv::SnapshotCsv, dir::SnapshotDir, json_model::SnapshotJsonModel, lua::SnapshotLua,
project::SnapshotProject, rbxlx::SnapshotRbxlx, rbxm::SnapshotRbxm, rbxmx::SnapshotRbxmx,
txt::SnapshotTxt,
};
use crate::snapshot::InstanceContext; use crate::snapshot::InstanceContext;
use self::{
csv::SnapshotCsv,
dir::SnapshotDir,
json_model::SnapshotJsonModel,
lua::SnapshotLua,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
project::SnapshotProject,
rbxlx::SnapshotRbxlx,
rbxm::SnapshotRbxm,
rbxmx::SnapshotRbxmx,
txt::SnapshotTxt,
};
pub use self::error::*;
pub use self::project::snapshot_project_node; pub use self::project::snapshot_project_node;
macro_rules! middlewares { macro_rules! middlewares {