Switch from failure to anyhow and thiserror. (#863)

* Switch to `anyhow` and `thiserror`.

* Fix doc tests.

* Fix fmt.

* Some polish.
This commit is contained in:
daxpedda 2020-01-15 14:55:09 +01:00 committed by Justin Starry
parent a4423122de
commit 1f4c415eac
12 changed files with 36 additions and 36 deletions

View File

@ -19,9 +19,9 @@ description = "A framework for making client-side single-page apps"
travis-ci = { repository = "yewstack/yew" }
[dependencies]
anyhow = "1"
anymap = "0.12"
bincode = { version = "~1.2.1", optional = true }
failure = "0.1"
http = "0.2"
indexmap = "1.0.2"
log = "0.4"
@ -34,6 +34,7 @@ serde_json = "1.0"
serde_yaml = { version = "0.8.3", optional = true }
slab = "0.4"
stdweb = "0.4.20"
thiserror = "1"
toml = { version = "0.5", optional = true }
yew-macro = { version = "0.11.1", path = "crates/macro" }

View File

@ -5,7 +5,7 @@ authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
edition = "2018"
[dependencies]
failure = "0.1"
anyhow = "1"
serde = "1"
serde_derive = "1"
yew = { path = "../..", features = ["toml"] }

View File

@ -1,6 +1,6 @@
#![recursion_limit = "256"]
use failure::Error;
use anyhow::Error;
use serde_derive::{Deserialize, Serialize};
use yew::format::{Json, Nothing, Toml};
use yew::services::fetch::{FetchService, FetchTask, Request, Response};

View File

@ -5,7 +5,7 @@ authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
edition = "2018"
[dependencies]
failure = "0.1"
anyhow = "1"
serde = "1"
serde_derive = "1"
stdweb = "0.4.20"

View File

@ -1,4 +1,4 @@
use failure::{format_err, Error};
use anyhow::{anyhow, Error};
use serde_derive::Deserialize;
use yew::callback::Callback;
use yew::format::{Json, Nothing};
@ -38,8 +38,7 @@ impl GravatarService {
if meta.status.is_success() {
callback.emit(data)
} else {
// format_err! is a macro in crate `failure`
callback.emit(Err(format_err!(
callback.emit(Err(anyhow!(
"{}: error getting profile https://gravatar.com/",
meta.status
)))

View File

@ -7,7 +7,7 @@ extern crate stdweb;
pub mod ccxt;
pub mod gravatar;
use failure::Error;
use anyhow::Error;
use yew::services::fetch::FetchTask;
use yew::{html, Callback, Component, ComponentLink, Html, ShouldRender};

View File

@ -7,17 +7,17 @@ macro_rules! text_format {
T: ::serde::Serialize,
{
fn into(self) -> $crate::format::Text {
$format::to_string(&self.0).map_err(::failure::Error::from)
$format::to_string(&self.0).map_err(::anyhow::Error::from)
}
}
impl<T> From<$crate::format::Text> for $type<Result<T, ::failure::Error>>
impl<T> From<$crate::format::Text> for $type<Result<T, ::anyhow::Error>>
where
T: for<'de> ::serde::Deserialize<'de>,
{
fn from(value: $crate::format::Text) -> Self {
match value {
Ok(data) => $type($format::from_str(&data).map_err(::failure::Error::from)),
Ok(data) => $type($format::from_str(&data).map_err(::anyhow::Error::from)),
Err(reason) => $type(Err(reason)),
}
}
@ -35,17 +35,17 @@ macro_rules! binary_format {
T: ::serde::Serialize,
{
fn into(self) -> $crate::format::Binary {
$into(&self.0).map_err(::failure::Error::from)
$into(&self.0).map_err(::anyhow::Error::from)
}
}
impl<T> From<$crate::format::Binary> for $type<Result<T, ::failure::Error>>
impl<T> From<$crate::format::Binary> for $type<Result<T, ::anyhow::Error>>
where
T: for<'de> ::serde::Deserialize<'de>,
{
fn from(value: $crate::format::Binary) -> Self {
match value {
Ok(data) => $type($from(&data).map_err(::failure::Error::from)),
Ok(data) => $type($from(&data).map_err(::anyhow::Error::from)),
Err(reason) => $type(Err(reason)),
}
}
@ -57,11 +57,11 @@ macro_rules! text_format_is_an_error {
($type:ident) => {
use $crate::format::FormatError;
fn to_string<T>(_value: T) -> Result<String, failure::Error> {
fn to_string<T>(_value: T) -> Result<String, ::anyhow::Error> {
Err(FormatError::CantEncodeBinaryAsText.into())
}
fn from_str<T>(_s: &str) -> Result<T, failure::Error> {
fn from_str<T>(_s: &str) -> Result<T, ::anyhow::Error> {
Err(FormatError::ReceivedTextForBinary.into())
}

View File

@ -4,8 +4,8 @@
//! All types here are lazy and it's necessary to
//! use `Into` and `From` traits to get (convert) the data.
use failure::Error;
use failure::Fail;
use anyhow::Error;
use thiserror::Error as ThisError;
#[macro_use]
pub mod macros;
@ -54,14 +54,14 @@ pub type Binary = Result<Vec<u8>, Error>;
pub type Format<T> = Result<T, Error>;
/// Represents formatting errors.
#[derive(Debug, Fail)]
#[derive(Debug, ThisError)]
pub enum FormatError {
/// Received text for a binary format, e.g. someone sending text
/// on a WebSocket that is using a binary serialization format, like Cbor.
#[fail(display = "received text for a binary format")]
#[error("received text for a binary format")]
ReceivedTextForBinary,
/// Trying to encode a binary format as text", e.g., trying to
/// store a Cbor encoded value in a String.
#[fail(display = "trying to encode a binary format as Text")]
#[error("trying to encode a binary format as Text")]
CantEncodeBinaryAsText,
}

View File

@ -1,7 +1,7 @@
//! Contains an implementation of empty serialization format (`Nothing`).
use super::{Binary, Text};
use failure::err_msg;
use anyhow::bail;
/// A representation of an empty data. Nothing stored. Nothing restored.
#[derive(Debug)]
@ -9,7 +9,7 @@ pub struct Nothing;
impl Into<Text> for Nothing {
fn into(self) -> Text {
Err(err_msg("nothing"))
bail!("nothing")
}
}
@ -21,7 +21,7 @@ impl From<Text> for Nothing {
impl Into<Binary> for Nothing {
fn into(self) -> Binary {
Err(err_msg("nothing"))
bail!("nothing")
}
}

View File

@ -3,7 +3,6 @@
use super::Task;
use crate::callback::Callback;
use crate::format::{Binary, Format, Text};
use failure::Fail;
use serde::Serialize;
use std::collections::HashMap;
use std::fmt;
@ -13,6 +12,7 @@ use stdweb::web::ArrayBuffer;
use stdweb::{JsSerialize, Value};
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
use thiserror::Error;
pub use http::{HeaderMap, Method, Request, Response, StatusCode, Uri};
@ -90,9 +90,9 @@ pub struct FetchOptions {
}
/// Represents errors of a fetch service.
#[derive(Debug, Fail)]
#[derive(Debug, Error)]
enum FetchError {
#[fail(display = "failed response")]
#[error("failed response")]
FailedResponse,
}
@ -157,10 +157,10 @@ impl FetchService {
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
///# let mut fetch_service: FetchService = FetchService::new();
///# let post_request: Request<Result<String, failure::Error>> = unimplemented!();
///# let post_request: Request<Result<String, anyhow::Error>> = unimplemented!();
/// let task = fetch_service.fetch(
/// post_request,
/// link.callback(|response: Response<Result<String, failure::Error>>| {
/// link.callback(|response: Response<Result<String, anyhow::Error>>| {
/// if response.status().is_success() {
/// Msg::Noop
/// } else {
@ -201,7 +201,7 @@ impl FetchService {
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
/// let get_request = Request::get("/thing").body(Nothing).unwrap();
/// let callback = link.callback(|response: Response<Json<Result<Data, failure::Error>>>| {
/// let callback = link.callback(|response: Response<Json<Result<Data, anyhow::Error>>>| {
/// if let (meta, Json(Ok(body))) = response.into_parts() {
/// if meta.status.is_success() {
/// return Msg::FetchResourceComplete(body);
@ -245,7 +245,7 @@ impl FetchService {
///# pub enum Msg {}
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
///# let callback = link.callback(|response: Response<Result<String, failure::Error>>| unimplemented!());
///# let callback = link.callback(|response: Response<Result<String, anyhow::Error>>| unimplemented!());
/// let request = fetch::Request::get("/path/")
/// .body(Nothing)
/// .unwrap();

View File

@ -2,14 +2,14 @@
//! use local and session storage of a browser.
use crate::format::Text;
use failure::Fail;
use std::fmt;
use stdweb::web::{window, Storage};
use thiserror::Error;
/// Represents errors of a storage.
#[derive(Debug, Fail)]
#[derive(Debug, Error)]
enum StorageError {
#[fail(display = "restore error")]
#[error("restore error")]
CantRestore,
}

View File

@ -1,6 +1,6 @@
//! This module contains useful utils to get information about the current document.
use failure::{err_msg, Error};
use anyhow::{anyhow, Error};
use std::marker::PhantomData;
use stdweb::web::document;
@ -8,7 +8,7 @@ use stdweb::web::document;
pub fn host() -> Result<String, Error> {
document()
.location()
.ok_or_else(|| err_msg("can't get location"))
.ok_or_else(|| anyhow!("can't get location"))
.and_then(|l| l.host().map_err(Error::from))
}