Repository: rust-lang-deprecated/failure (language: rust)

Issue:
The Error's backtrace ought to be an Option
Allowing to check:

```rust
if err.backtrace().is_some() {
    ...
}
```

To decide about printing the backtrace or not, is more idiomatic no? for now, I need to check if RUST_BACKTRACE is set.


Discussion / hints:
What's wrong with printing it unconditionally? It's not an option because in `Fail`, the optionality is connected to whether this error type has a backtrace, not whether or not `RUST_BACKTRACE` is set. I just figured you'd always print it and it'd print nothing if backtraces are turned off.
In my case, I print errors — and backtraces — with `log` macros, which add timestamps and such to the print. If backtraces are disabled I get a line like `ERROR 2018-02-12T22:07:45Z: main_app: ` with no backtrace info attached to it.
That's annoying.

Currently there's a private `is_none` method on Backtrace. Maybe we could expose that (probably under a different name).
I'd use `is_empty` so it is idiomatic. 

Fix patch:
diff --git a/src/backtrace/mod.rs b/src/backtrace/mod.rs
index 2d3ab8d..9eba16d 100644
--- a/src/backtrace/mod.rs
+++ b/src/backtrace/mod.rs
@@ -52,6 +52,16 @@ without_backtrace! {
         pub(crate) fn is_none(&self) -> bool {
             true
         }
+
+        /// Returns true if displaying this backtrace would be an empty string.
+        ///
+        /// > (We have detected that this crate was documented with no_std
+        /// > compatibility turned on. The version of this crate that has been
+        /// > documented here will never generate a backtrace and this method
+        /// > will always return true.)
+        pub fn is_empty(&self) -> bool {
+            true
+        }
     }
 
     impl Default for Backtrace {
@@ -118,6 +128,11 @@ with_backtrace! {
         pub(crate) fn is_none(&self) -> bool {
             self.internal.is_none()
         }
+
+        /// Returns true if displaying this backtrace would be an empty string.
+        pub fn is_empty(&self) -> bool {
+            self.internal.is_none()
+        }
     }
 
     impl Default for Backtrace {


Test patch:
diff --git a/failure_derive/tests/wraps.rs b/failure_derive/tests/wraps.rs
index 38218a2..2049273 100644
--- a/failure_derive/tests/wraps.rs
+++ b/failure_derive/tests/wraps.rs
@@ -58,6 +58,8 @@ fn wrap_backtrace_error() {
         .and_then(|err| err.downcast_ref::<io::Error>())
         .is_some());
     assert!(err.backtrace().is_some());
+    assert!(err.backtrace().is_empty());
+    assert_eq!(err.backtrace().is_empty(), err.backtrace().to_string().trim().is_empty());
 }
 
 #[derive(Fail, Debug)]
@@ -91,4 +93,6 @@ fn wrap_enum_error() {
         .and_then(|err| err.downcast_ref::<fmt::Error>())
         .is_some());
     assert!(err.backtrace().is_some());
+    assert!(err.backtrace().is_empty());
+    assert_eq!(err.backtrace().is_empty(), err.backtrace().to_string().trim().is_empty());
 }