| 1 |
// Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved |
| 2 |
// Copyright (c) 2017-2020, The rav1e contributors. All rights reserved |
| 3 |
// |
| 4 |
// This source code is subject to the terms of the BSD 2 Clause License and |
| 5 |
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 |
// was not distributed with this source code in the LICENSE file, you can |
| 7 |
// obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 |
// Media Patent License 1.0 was not distributed with this source code in the |
| 9 |
// PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 |
|
| 11 |
#[derive(Debug, thiserror::Error)] |
| 12 |
pub enum CliError { |
| 13 |
#[error("Cannot parse option `{opt}`: {err}")] |
| 14 |
ParseInt { opt: String, err: std::num::ParseIntError }, |
| 15 |
#[error("{msg}")] |
| 16 |
Message { msg: String }, |
| 17 |
#[error("{msg}: {e}")] |
| 18 |
Generic { msg: String, e: String }, |
| 19 |
} |
| 20 |
|
| 21 |
impl CliError { |
| 22 |
pub fn new(msg: &str) -> CliError { |
| 23 |
CliError::Message { msg: msg.to_owned() } |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
pub trait ToError: std::error::Error + Sized { |
| 28 |
fn context(self, msg: &str) -> CliError { |
| 29 |
CliError::Generic { msg: msg.to_owned(), e: self.to_string() } |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl ToError for std::num::ParseIntError { |
| 34 |
fn context(self, opt: &str) -> CliError { |
| 35 |
CliError::ParseInt { opt: opt.to_lowercase(), err: self } |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
impl ToError for std::io::Error {} |
| 40 |
impl ToError for rav1e::InvalidConfig {} |
| 41 |
impl ToError for rav1e::EncoderStatus {} |
| 42 |
impl ToError for rav1e::config::RateControlError {} |
| 43 |
|
| 44 |
pub fn print_error(e: &dyn std::error::Error) { |
| 45 |
error!("{}", e); |
| 46 |
let mut cause = e.source(); |
| 47 |
while let Some(e) = cause { |
| 48 |
error!("Caused by: {}", e); |
| 49 |
cause = e.source(); |
| 50 |
} |
| 51 |
} |