Skip to content

Commit 67c267a

Browse files
feat(errors): Print backtrace when RUST_BACKTRACE=1 (#2189)
The anyhow crate we use for error handling adds stack backtraces errors whenever certain standard environment variables are set (e.g. when RUST_BACKTRACE=1). Modify our error-printing logic to include these stack backtraces when printing errors that crash Sentry CLI, so that users can add this information to bug reports. Closes #2187
1 parent 7160d52 commit 67c267a

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

src/utils/system.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ pub fn print_error(err: &Error) {
7676
clap_err.exit();
7777
}
7878

79-
eprintln!("{} {}", style("error:").red(), err);
80-
err.chain()
81-
.skip(1)
82-
.for_each(|cause| eprintln!(" {} {}", style("caused by:").dim(), cause));
79+
// Debug style for error includes cause chain and backtrace (if available).
80+
eprintln!("{} {:?}", style("error:").red(), err);
8381

8482
if Config::current_opt().map_or(true, |config| {
8583
config.get_log_level() < log::LevelFilter::Info

tests/integration/_cases/debug_files/debug_files-bundle-jvm-output-is-file.trycmd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ $ sentry-cli debug-files bundle-jvm --output ./file.txt --debug-id D384DC3B-AB2F
55
> Bundled 2 files for upload
66
> Bundle ID: [..]-[..]-[..]-[..]-[..]
77
error: Unable to write source bundle
8-
caused by: [..]
8+
9+
Caused by:
10+
[..]
911

1012
Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.
1113
Please attach the full debug log to all bug reports.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
```
2+
$ RUST_BACKTRACE=1 sentry-cli info
3+
? failed
4+
Sentry Server: https://sentry.io
5+
Default Organization: -
6+
Default Project: -
7+
8+
Authentication Info:
9+
Method: Unauthorized
10+
error: Auth token is required for this request. Please run `sentry-cli login` and try again!
11+
12+
Stack backtrace:
13+
...
14+
15+
Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.
16+
Please attach the full debug log to all bug reports.
17+
18+
```

tests/integration/_cases/releases/releases-delete-active.trycmd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
$ sentry-cli releases delete wat-release
33
? failed
44
error: API request failed
5-
caused by: sentry reported an error: This release is referenced by active issues and cannot be removed. (http status: 400)
5+
6+
Caused by:
7+
[..]sentry reported an error: This release is referenced by active issues and cannot be removed. (http status: 400)
68

79
Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.
810
Please attach the full debug log to all bug reports.

tests/integration/_cases/send_metric/send_metric-increment-unsuccessful-api-call.trycmd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ $ sentry-cli send-metric increment -n testmetric
33
? failed
44
[..]
55
error: API request failed
6-
caused by: sentry reported an error: internal server error (http status: 500)
6+
7+
Caused by:
8+
[..]sentry reported an error: internal server error (http status: 500)
79

810
Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.
911
Please attach the full debug log to all bug reports.

tests/integration/info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ fn command_info_no_token() {
1616
.case("tests/integration/_cases/info/info-no-token.trycmd");
1717
}
1818

19+
#[test]
20+
fn command_info_no_token_backtrace() {
21+
// Special case where we don't want any env variables set, so we don't use `register_task` helper.
22+
TestCases::new()
23+
.env("SENTRY_INTEGRATION_TEST", "1")
24+
.env("RUST_BACKTRACE", "1")
25+
.case("tests/integration/_cases/info/info-no-token-backtrace.trycmd");
26+
}
27+
1928
#[test]
2029
fn command_info_basic() {
2130
let _server = mock_endpoint(

0 commit comments

Comments
 (0)