Skip to content

Commit

Permalink
Remove rpc backtrace, resolve #4698
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Nov 26, 2024
1 parent 80cfe48 commit 677ce79
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ pub enum RPCError {
Indexer = -1200,
}

/// Removes the backtrace portion from an error string.
///
/// This function processes the input string `err_str` line by line and collects all lines
/// until it encounters a line starting with "Stack backtrace:". It then joins the collected
/// lines into a single string, effectively removing the backtrace section.
///
/// # Arguments
/// * `err_str` - A string slice containing the error message, potentially including a backtrace.
///
/// # Returns
/// * A `String` containing the error message without the backtrace.
fn remove_backtrace(err_str: &str) -> String {
let lines: Vec<_> = err_str
.lines()
.take_while(|line| !line.starts_with("Stack backtrace:"))
.collect();
lines.join("\n")
}

impl RPCError {
/// Invalid method parameter(s).
pub fn invalid_params<T: Display>(message: T) -> Error {
Expand Down Expand Up @@ -158,10 +177,12 @@ impl RPCError {
/// The parameter `err` is usually an std error. The Display form is used as the error message,
/// and the Debug form is used as the data.
pub fn custom_with_error<T: Display + Debug>(error_code: RPCError, err: T) -> Error {
let err_str_with_backtrace = format!("{err:?}");
let err_str = remove_backtrace(&err_str_with_backtrace);
Error {
code: ErrorCode::ServerError(error_code as i64),
message: format!("{error_code:?}: {err}"),
data: Some(Value::String(format!("{err:?}"))),
data: Some(Value::String(err_str)),
}
}

Expand Down

0 comments on commit 677ce79

Please sign in to comment.