Skip to content

Commit 2f1e697

Browse files
committed
Add a few more tests for chain_err
1 parent e86de9b commit 2f1e697

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/util/errors.rs

+38
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,42 @@ fn chain_error_internal() {
361361
.to_string(),
362362
"outer caused by inner"
363363
);
364+
365+
// Don't do this, the user will see a generic 500 error instead of the intended message
366+
assert_eq!(
367+
Err::<(), _>(cargo_err("inner"))
368+
.chain_error(|| internal("outer"))
369+
.unwrap_err()
370+
.to_string(),
371+
"outer caused by inner"
372+
);
373+
assert_eq!(
374+
Err::<(), _>(Unauthorized)
375+
.chain_error(|| internal("outer"))
376+
.unwrap_err()
377+
.to_string(),
378+
"outer caused by must be logged in to perform that action"
379+
);
380+
}
381+
382+
#[test]
383+
fn chain_error_user_facing() {
384+
// Do this rarely, the user will only see the outer error
385+
assert_eq!(
386+
Err::<(), _>(cargo_err("inner"))
387+
.chain_error(|| cargo_err("outer"))
388+
.unwrap_err()
389+
.to_string(),
390+
"outer caused by inner" // never logged
391+
);
392+
393+
// The outer error is sent as a response to the client.
394+
// The inner error never bubbles up to the logging middleware
395+
assert_eq!(
396+
Err::<(), _>(std::io::Error::from(std::io::ErrorKind::PermissionDenied))
397+
.chain_error(|| cargo_err("outer"))
398+
.unwrap_err()
399+
.to_string(),
400+
"outer caused by permission denied" // never logged
401+
);
364402
}

0 commit comments

Comments
 (0)