Skip to content

Commit 66b9072

Browse files
committed
Improve SSLError message
1 parent a8a5094 commit 66b9072

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

stdlib/src/ssl.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,19 +978,30 @@ fn ssl_error(vm: &VirtualMachine) -> PyTypeRef {
978978
vm.class("_ssl", "SSLError")
979979
}
980980

981+
#[track_caller]
981982
fn convert_openssl_error(vm: &VirtualMachine, err: ErrorStack) -> PyBaseExceptionRef {
982983
let cls = ssl_error(vm);
983984
match err.errors().last() {
984985
Some(e) => {
986+
let caller = std::panic::Location::caller();
987+
let (file, line) = (caller.file(), caller.line());
988+
let file = file
989+
.rsplit_once(&['/', '\\'][..])
990+
.map_or(file, |(_, basename)| basename);
985991
// TODO: map the error codes to code names, e.g. "CERTIFICATE_VERIFY_FAILED", just requires a big hashmap/dict
986992
let errstr = e.reason().unwrap_or("unknown error");
987-
let msg = format!("{} (_ssl.c:{})", errstr, e.line());
993+
let msg = if let Some(lib) = e.library() {
994+
format!("[{}] {} ({}:{})", lib, errstr, file, line)
995+
} else {
996+
format!("{} ({}:{})", errstr, file, line)
997+
};
988998
let reason = sys::ERR_GET_REASON(e.code());
989999
vm.new_exception(cls, vec![vm.ctx.new_int(reason), vm.ctx.new_utf8_str(msg)])
9901000
}
9911001
None => vm.new_exception_empty(cls),
9921002
}
9931003
}
1004+
#[track_caller]
9941005
fn convert_ssl_error(
9951006
vm: &VirtualMachine,
9961007
e: impl std::borrow::Borrow<ssl::Error>,

0 commit comments

Comments
 (0)