Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove anyhow's backtrace from RPC response #4728

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ pub enum RPCError {
Indexer = -1200,
}

/// Removes the backtrace portion from an error string.
fn remove_backtrace(err_str: &str) -> &str {
match err_str.find("\nStack backtrace:") {
Some(idx) => &err_str[..idx],
None => err_str,
}
}

impl RPCError {
/// Invalid method parameter(s).
pub fn invalid_params<T: Display>(message: T) -> Error {
Expand Down Expand Up @@ -158,10 +166,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.to_string())),
}
}

Expand Down
Loading