Skip to content

Commit e0a935f

Browse files
authored
Allow to create Backtrace (#795)
1 parent e713098 commit e0a935f

File tree

10 files changed

+52
-17
lines changed

10 files changed

+52
-17
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ ntex-util = { path = "ntex-util" }
5656
ntex = "3.6.0"
5757
ntex-bytes = "1.5.2"
5858
ntex-codec = "1.1.0"
59-
ntex-error = "1.0.0"
59+
ntex-error = "1.0.1"
6060
ntex-io = "3.9.1"
6161
ntex-dispatcher = "3.1.0"
62-
ntex-net = "3.8.0"
62+
ntex-net = "3.8.1"
6363
ntex-http = "1.0.0"
6464
ntex-router = "1.0.0"
65-
ntex-rt = "3.10.0"
65+
ntex-rt = "3.10.1"
6666
ntex-server = "3.9.0"
6767
ntex-service = "4.6.0"
6868
ntex-tls = "3.4.0"

ntex-error/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [1.0.1] - 2026-03-08
4+
5+
* Allow to create Backtrace
6+
37
## [1.0.0] - 2026-03-07
48

59
* Initial error management implementation

ntex-error/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-error"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "ntex error management"
66
keywords = ["network", "framework"]

ntex-error/src/lib.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ thread_local! {
1414
static REPRS: RefCell<HashMap<u64, Arc<str>>> = RefCell::new(HashMap::default());
1515
}
1616
static mut START: Option<(&'static str, u32)> = None;
17+
static mut START_ALT: Option<(&'static str, u32)> = None;
1718

18-
#[track_caller]
1919
pub fn set_backtrace_start(file: &'static str, line: u32) {
2020
unsafe {
2121
START = Some((file, line));
2222
}
2323
}
2424

25+
#[doc(hidden)]
26+
pub fn set_backtrace_start_alt(file: &'static str, line: u32) {
27+
unsafe {
28+
START_ALT = Some((file, line));
29+
}
30+
}
31+
2532
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2633
pub enum ErrorType {
2734
Client,
@@ -83,7 +90,7 @@ pub trait ErrorDiagnostic: error::Error + 'static {
8390
}
8491
}
8592

86-
#[derive(Debug, Clone)]
93+
#[derive(Clone)]
8794
pub struct Error<E> {
8895
inner: Box<ErrorInner<E>>,
8996
}
@@ -358,7 +365,8 @@ where
358365
pub struct Backtrace(Arc<str>);
359366

360367
impl Backtrace {
361-
fn new(loc: &Location<'_>) -> Self {
368+
/// Create new backtrace
369+
pub fn new(loc: &Location<'_>) -> Self {
362370
let repr = FRAMES.with(|c| {
363371
let mut cache = c.borrow_mut();
364372
let mut idx = 0;
@@ -396,8 +404,13 @@ impl Backtrace {
396404
find_loc(loc, &mut frames);
397405

398406
#[allow(static_mut_refs)]
399-
if let Some(start) = unsafe { START } {
400-
find_loc_start(start, &mut frames);
407+
{
408+
if let Some(start) = unsafe { START } {
409+
find_loc_start(start, &mut frames);
410+
}
411+
if let Some(start) = unsafe { START_ALT } {
412+
find_loc_start(start, &mut frames);
413+
}
401414
}
402415

403416
let bt = Bt(&frames[..]);
@@ -441,26 +454,27 @@ fn find_loc(loc: &Location<'_>, frames: &mut [Option<&BacktraceFrame>]) {
441454
}
442455

443456
fn find_loc_start(loc: (&str, u32), frames: &mut [Option<&BacktraceFrame>]) {
444-
let mut idx = frames.len();
445-
while idx > 0 {
446-
idx -= 1;
457+
let mut idx = 0;
458+
while idx < frames.len() {
447459
if let Some(frm) = &frames[idx] {
448460
for sym in frm.symbols() {
449461
if let Some(fname) = sym.filename()
450462
&& let Some(lineno) = sym.lineno()
451463
&& fname.ends_with(loc.0)
452-
&& lineno == loc.1
464+
&& (loc.1 == 0 || lineno == loc.1)
453465
{
454-
for f in frames.iter_mut().skip(idx + 1) {
466+
for f in frames.iter_mut().skip(idx) {
455467
if f.is_some() {
456468
*f = None;
457469
} else {
458470
return;
459471
}
460472
}
473+
break;
461474
}
462475
}
463476
}
477+
idx += 1;
464478
}
465479
}
466480

@@ -502,6 +516,19 @@ impl fmt::Display for Backtrace {
502516
}
503517
}
504518

519+
impl<E> fmt::Debug for Error<E>
520+
where
521+
E: ErrorDiagnostic,
522+
{
523+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
524+
f.debug_struct("Error")
525+
.field("error", &self.inner.error)
526+
.field("service", &self.inner.service)
527+
.field("backtrace", &self.inner.backtrace)
528+
.finish()
529+
}
530+
}
531+
505532
impl<E> fmt::Display for Error<E>
506533
where
507534
E: ErrorDiagnostic,

ntex-net/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-net"
3-
version = "3.8.0"
3+
version = "3.8.1"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "ntexwork utils for ntex framework"
66
keywords = ["network", "framework", "async", "futures"]

ntex-net/src/compio/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) fn block_on<F: Future<Output = ()>>(fut: F) {
2323
compio_runtime::Runtime::try_with_current(Runtime::driver_type)
2424
.unwrap_or(compio_driver::DriverType::Poll)
2525
);
26+
ntex_error::set_backtrace_start_alt("src/future/future/catch_unwind.rs", 0);
2627
let rt = Runtime::new().unwrap();
2728
rt.block_on(fut);
2829
}

ntex-net/src/tokio/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub(crate) fn block_on<F: Future<Output = ()>>(fut: F) {
3333
.enable_all()
3434
.build()
3535
.unwrap();
36+
37+
ntex_error::set_backtrace_start_alt("src/runtime/task/core.rs", 0);
3638
tok_io::task::LocalSet::new().block_on(&rt, fut);
3739
}
3840
}

ntex-rt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-rt"
3-
version = "3.10.0"
3+
version = "3.10.1"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "ntex runtime"
66
keywords = ["network", "framework", "async", "futures"]

ntex-rt/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl SystemRunner {
228228
}
229229

230230
#[track_caller]
231-
fn current_location() -> &'static panic::Location<'static> {
231+
pub(crate) fn current_location() -> &'static panic::Location<'static> {
232232
panic::Location::caller()
233233
}
234234

ntex-rt/src/rt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl Runtime {
118118
});
119119
}
120120

121+
ntex_error::set_backtrace_start_alt("src/raw.rs", 0);
121122
driver.run(self).expect("Driver failed");
122123
result.expect("Driver failed to poll")
123124
})

0 commit comments

Comments
 (0)