Does the error line number not automatically appear in the result?

⚓ Rust    📅 2026-02-19    👤 surdeus    👁️ 6      

surdeus

I tried it before, and the error info didn't show which line it was on. Even when using thiserror and anyhow, it didn't show up. Is it supposed to be like that?

I tried to manually capture the backtrace to get the line number info

use std::borrow::Cow;
use std::fmt;
use std::error::Error as StdError;
use std::io::Error as StdIoError;

#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;

#[derive(Copy, Clone)]
enum Error {
    WrongInput,
    IoError
}

const ERROR: &[&'static str] = &[
    "Wrong Input",
    "IO Error"
];

struct AppError {
    error: Error,
    detail: Cow<'static, str>,
    
    #[cfg(feature = "backtrace")]
    backtrace: Backtrace
}

impl AppError {
    fn new<T>(error: Error, detail: T) -> Self 
    where
        T: Into<Cow<'static, str>>
    {
        Self { 
            error,
            detail: detail.into(),
            
            #[cfg(feature = "backtrace")]
            backtrace: Backtrace::capture()
        }
    }
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let error = ERROR[self.error as usize];
        
        #[cfg(not(feature = "backtrace"))]
        return write!(f, "{} {}", error, self.detail);
        
        #[cfg(feature = "backtrace")]
        return write!(f, "{} {}\nBacktrace : {}", error, self.detail, self.backtrace);
    }
}

impl fmt::Debug for AppError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    fmt::Display::fmt(self, f)
  }
}

impl StdError for AppError {}

impl From<StdIoError> for AppError {
    fn from(err: StdIoError) -> Self {
        AppError::new(Error::IoError, err.to_string())
    }
}

fn test(input: i32) -> Result<i32, AppError> {
    if input % 2 == 0 {
        return Ok(input * 2);
    }
    
    Err(AppError::new(Error::WrongInput, "Input is not divisible by 2"))
}

fn main() -> Result<(), AppError> {
    let _test = test(1)?;
    
    let _test2 = std::fs::read("no_file.txt")?;
    
    Ok(())
}

Without Backtrace capture :

RUST_BACKTRACE=1 cargo run      
     
Error: Wrong Input Input is not divisible by 2   

With Backtrace capture :

RUST_BACKTRACE=1 cargo run --features backtrace

Error: Wrong Input Input is not divisible by 2
Backtrace :    0: tes::AppError::new
             at ./src/main.rs:38:24                      1: tes::test
             at ./src/main.rs:74:9
   2: tes::main                                                    at ./src/main.rs:78:17
   3: core::ops::function::FnOnce::call_once
             at /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
   4: std::sys::backtrace::__rust_begin_short_backtrace                                                                  at /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:158:18
   5: std::rt::lang_start::{{closure}}
             at /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:206:18
   6: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/ops/function.rs:287:21
   7: std::panicking::catch_unwind::do_call
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:590:40
   8: std::panicking::catch_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:553:19
   9: std::panic::catch_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panic.rs:359:14
  10: std::rt::lang_start_internal::{{closure}}
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/rt.rs:175:24
  11: std::panicking::catch_unwind::do_call
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:590:40
  12: std::panicking::catch_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:553:19
  13: std::panic::catch_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panic.rs:359:14
  14: std::rt::lang_start_internal
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/rt.rs:171:5
  15: std::rt::lang_start
             at /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:205:5
  16: main
  17: <unknown>
  18: __libc_start_main
  19: _start

Also is there anything missing or wrong with my error handling approach?

5 posts - 4 participants

Read full topic

🏷️ Rust_feed