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" } travis-ci = { repository = "yewstack/yew" }
[dependencies] [dependencies]
anyhow = "1"
anymap = "0.12" anymap = "0.12"
bincode = { version = "~1.2.1", optional = true } bincode = { version = "~1.2.1", optional = true }
failure = "0.1"
http = "0.2" http = "0.2"
indexmap = "1.0.2" indexmap = "1.0.2"
log = "0.4" log = "0.4"
@ -34,6 +34,7 @@ serde_json = "1.0"
serde_yaml = { version = "0.8.3", optional = true } serde_yaml = { version = "0.8.3", optional = true }
slab = "0.4" slab = "0.4"
stdweb = "0.4.20" stdweb = "0.4.20"
thiserror = "1"
toml = { version = "0.5", optional = true } toml = { version = "0.5", optional = true }
yew-macro = { version = "0.11.1", path = "crates/macro" } yew-macro = { version = "0.11.1", path = "crates/macro" }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,17 +7,17 @@ macro_rules! text_format {
T: ::serde::Serialize, T: ::serde::Serialize,
{ {
fn into(self) -> $crate::format::Text { 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 where
T: for<'de> ::serde::Deserialize<'de>, T: for<'de> ::serde::Deserialize<'de>,
{ {
fn from(value: $crate::format::Text) -> Self { fn from(value: $crate::format::Text) -> Self {
match value { 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)), Err(reason) => $type(Err(reason)),
} }
} }
@ -35,17 +35,17 @@ macro_rules! binary_format {
T: ::serde::Serialize, T: ::serde::Serialize,
{ {
fn into(self) -> $crate::format::Binary { 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 where
T: for<'de> ::serde::Deserialize<'de>, T: for<'de> ::serde::Deserialize<'de>,
{ {
fn from(value: $crate::format::Binary) -> Self { fn from(value: $crate::format::Binary) -> Self {
match value { 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)), Err(reason) => $type(Err(reason)),
} }
} }
@ -57,11 +57,11 @@ macro_rules! text_format_is_an_error {
($type:ident) => { ($type:ident) => {
use $crate::format::FormatError; 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()) 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()) Err(FormatError::ReceivedTextForBinary.into())
} }

View File

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

View File

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

View File

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

View File

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

View File

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