mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-12-07 16:22:31 -05:00
replace homegrown Error with failure crate
This reduces boilerplate, making it a bit easier for me to split the db stuff out into its own crate.
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
//! Subcommand to check the database and sample file dir for errors.
|
||||
|
||||
use db;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use recording;
|
||||
use std::fs;
|
||||
use uuid::Uuid;
|
||||
@@ -153,9 +153,7 @@ pub fn run() -> Result<(), Error> {
|
||||
error!("composite id {} has recording_playback row but no recording row", id2);
|
||||
continue;
|
||||
},
|
||||
(None, None) => {
|
||||
return Err(Error::new("outer join returned fully empty row".to_owned()));
|
||||
},
|
||||
(None, None) => bail!("outer join returned fully empty row"),
|
||||
};
|
||||
let row_summary = RecordingSummary{
|
||||
flags: row.get_checked(1)?,
|
||||
|
||||
@@ -35,7 +35,7 @@ use self::cursive::traits::{Boxable, Identifiable, Finder};
|
||||
use self::cursive::views;
|
||||
use db;
|
||||
use dir;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use stream::{self, Opener, Stream};
|
||||
|
||||
@@ -35,7 +35,7 @@ use self::cursive::traits::{Boxable, Identifiable};
|
||||
use self::cursive::views;
|
||||
use db;
|
||||
use dir;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::BTreeMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -38,7 +38,7 @@ extern crate cursive;
|
||||
use self::cursive::Cursive;
|
||||
use self::cursive::views;
|
||||
use db;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use regex::Regex;
|
||||
use std::sync::Arc;
|
||||
use std::fmt::Write;
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use db;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
|
||||
static USAGE: &'static str = r#"
|
||||
Initializes a database.
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
use dir;
|
||||
use docopt;
|
||||
use error::Error;
|
||||
use failure::{Error, Fail};
|
||||
use libc;
|
||||
use rusqlite;
|
||||
use std::path::Path;
|
||||
@@ -78,10 +78,8 @@ fn open_conn(db_dir: &str, mode: OpenMode) -> Result<(dir::Fd, rusqlite::Connect
|
||||
let dir = dir::Fd::open(None, db_dir, mode == OpenMode::Create)?;
|
||||
let ro = mode == OpenMode::ReadOnly;
|
||||
dir.lock(if ro { libc::LOCK_SH } else { libc::LOCK_EX } | libc::LOCK_NB)
|
||||
.map_err(|e| Error{description: format!("db dir {:?} already in use; can't get {} lock",
|
||||
db_dir,
|
||||
if ro { "shared" } else { "exclusive" }),
|
||||
cause: Some(Box::new(e))})?;
|
||||
.map_err(|e| e.context(format!("db dir {:?} already in use; can't get {} lock",
|
||||
db_dir, if ro { "shared" } else { "exclusive" })))?;
|
||||
let conn = rusqlite::Connection::open_with_flags(
|
||||
Path::new(&db_dir).join("db"),
|
||||
match mode {
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
use clock;
|
||||
use db;
|
||||
use dir;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use fnv::FnvHashMap;
|
||||
use futures::{Future, Stream};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use recording;
|
||||
|
||||
const USAGE: &'static str = r#"
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
/// See `guide/schema.md` for more information.
|
||||
|
||||
use db;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use rusqlite;
|
||||
|
||||
mod v0_to_v1;
|
||||
@@ -101,10 +101,10 @@ pub fn run() -> Result<(), Error> {
|
||||
let old_ver =
|
||||
conn.query_row("select max(id) from version", &[], |row| row.get_checked(0))??;
|
||||
if old_ver > db::EXPECTED_VERSION {
|
||||
return Err(Error::new(format!("Database is at version {}, later than expected {}",
|
||||
old_ver, db::EXPECTED_VERSION)))?;
|
||||
bail!("Database is at version {}, later than expected {}",
|
||||
old_ver, db::EXPECTED_VERSION);
|
||||
} else if old_ver < 0 {
|
||||
return Err(Error::new(format!("Database is at negative version {}!", old_ver)));
|
||||
bail!("Database is at negative version {}!", old_ver);
|
||||
}
|
||||
info!("Upgrading database from version {} to version {}...", old_ver, db::EXPECTED_VERSION);
|
||||
set_journal_mode(&conn, &args.flag_preset_journal).unwrap();
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
/// Upgrades a version 0 schema to a version 1 schema.
|
||||
|
||||
use db;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use recording;
|
||||
use rusqlite;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
/// Upgrades a version 1 schema to a version 2 schema.
|
||||
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use std::fs;
|
||||
use rusqlite;
|
||||
use schema::DirMeta;
|
||||
@@ -45,8 +45,8 @@ pub fn new<'a>(args: &'a super::Args) -> Result<Box<super::Upgrader + 'a>, Error
|
||||
let sample_file_path =
|
||||
args.flag_sample_file_dir
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::new("--sample-file-dir required when upgrading from \
|
||||
schema version 1 to 2.".to_owned()))?;
|
||||
.ok_or_else(|| format_err!("--sample-file-dir required when upgrading from \
|
||||
schema version 1 to 2."))?;
|
||||
Ok(Box::new(U { sample_file_path, dir_meta: None }))
|
||||
}
|
||||
|
||||
@@ -81,8 +81,7 @@ impl<'a> U<'a> {
|
||||
let row = row?;
|
||||
let uuid: ::db::FromSqlUuid = row.get_checked(0)?;
|
||||
if !files.contains(&uuid.0) {
|
||||
return Err(Error::new(format!("{} is missing from dir {}!",
|
||||
uuid.0, self.sample_file_path)));
|
||||
bail!("{} is missing from dir {}!", uuid.0, self.sample_file_path);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -318,7 +317,7 @@ fn fix_video_sample_entry(tx: &rusqlite::Transaction) -> Result<(), Error> {
|
||||
fn rfc6381_codec_from_sample_entry(sample_entry: &[u8]) -> Result<String, Error> {
|
||||
if sample_entry.len() < 99 || &sample_entry[4..8] != b"avc1" ||
|
||||
&sample_entry[90..94] != b"avcC" {
|
||||
return Err(Error::new("not a valid AVCSampleEntry".to_owned()));
|
||||
bail!("not a valid AVCSampleEntry");
|
||||
}
|
||||
let profile_idc = sample_entry[103];
|
||||
let constraint_flags_byte = sample_entry[104];
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
use db::{self, FromSqlUuid};
|
||||
use dir;
|
||||
use error::Error;
|
||||
use failure::Error;
|
||||
use libc;
|
||||
use std::io::{self, Write};
|
||||
use std::mem;
|
||||
|
||||
Reference in New Issue
Block a user