Comments (14)
FWIW I wrote a small helper to add the chain_err
method to futures, but there's likely other cool integration that can happen as well.
from error-chain.
Hello, I've made some changes to @alexcrichton helper to work with futures_cpupool::CpuPool
(since CpuPool::spawn
needs the future to have Send
trait implemented and for the ones I was testing it was failing.
Also, I've made a macro future_chain!
which works like error_chain!
(since I almost CTRL+C CTRL+V it) but only supports types
block.
With it you can just use future_chain!{}
after error_chain!{}
to generate a special FutureChain
type that works with error-chain
structure.
All the code is in this link: https://play.rust-lang.org/?gist=0df6f758e0f109fe64ad83cbf1ea7c2c&version=stable&backtrace=0
Note that Rust playground doesn't support crates, so you will need to create a local cargo projeto to it and add
[dependencies]
error-chain = "0.10.0"
futures = "0.1.9"
futures-cpupool = "0.1"
to it.
I hope this helps someone
from error-chain.
Maybe simply impl [ResultExt](https://docs.rs/error-chain/0.7.1/error_chain/example_generated/trait.ResultExt.html) for Future
?
from error-chain.
@Yamakaky Yeah, exactly. It's probably easy enough to do that it wouldn't be all that bad to just add that boilerplate into every project that uses Futures, but I'm wondering if Futures is going to be popular enough to warrant just adding support to the error_chain crate maybe as an optional feature.
from error-chain.
Just thinking that in the future we could use Carrier, when it's stabilized. It would apply to Future, Result and Option.
from error-chain.
It will definitively be an optional feature. Do you want to try?
from error-chain.
Good idea about using the Carrier
trait once it's stabilized. Yeah, I can take a stab at it and send a PR over.
from error-chain.
In fact, can't it be implemented in future
?
from error-chain.
You mean in the futures crate? I don't think it could really be implemented there since the trait is defined in the macro, right?
from error-chain.
My bad, my change was reverted few days ago ;)
from error-chain.
BTW, could Future
implement Carrier
?
from error-chain.
5 minutes ago, I didn't see any problems with that, but now I've realized that it probably can't work. The problem I see is that a Future
doesn't itself represent a result, so the ?
operator really wouldn't make any sense. You don't know whether the future represents a success or failure until after it's been completed, at which point it just returns a Result
.
from error-chain.
Do you have an example of code where you would like to use error-chain
?
from error-chain.
Sure. In the example, the methods in tokio_core::io
all return Future
s where the error type is std::io::Error
, but my_future_fn
returns a Future
with an associated error type of, MyError
.
let future = tokio_core::net::TcpStream::connect(&addr, &reactor.handle()).map_err(|err| {
format!("error creating connection: {:?}", err)
}).and_then(|tcp_stream| {
my_future_fn().map_err(|err| {
format!("Error in my_future_fn: {:?}", err)
}).and_then(|my_value| {
tokio_core::io::write_all(tcp_stream, my_value)
})
});
let result: Result<(TcpStream, Vec<u8>), String> = reactor.run(future);
In that example, both the io errors and the custom error types are being converted to strings since they need to be the same type in order for the chained calls to and_then
to work. A better way of doing this would of course be to convert them to a better common error type than String, but you get the picture. It just ends up being verbose having to do all the conversions manually in map_err
calls.
from error-chain.
Related Issues (20)
- If this crate is really drepecate, I should specify it and mark alternatives. HOT 8
- cargo publish error HOT 3
- deprecate quick_main?
- trait objects without an explicit `dyn` are deprecated
- changes to backtrace crate HOT 1
- Shouldn't src/bin/has_backtrace.rs be in examples? HOT 1
- How to implement complex display expression?
- backtrace name collision HOT 1
- use of deprecated try
- minimum version 1.13 for ? operator HOT 1
- How to force documentation when using error_chain HOT 5
- Move Repository To rust-lang
- lint error: use of deprecated item 'std::error::Error::description': use the Display impl or to_string() HOT 2
- Rust 2018 macro export pain HOT 3
- gcc error while testing HOT 3
- Release 0.12.2? HOT 8
- Chaining errors from spawned threads.
- Use feature specific links in error_chain! macro
- Add a feature gate to call DebugBreak on construction
- no rules expected this token in macro call HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from error-chain.