-
Notifications
You must be signed in to change notification settings - Fork 407
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
Improve error message #942
base: master
Are you sure you want to change the base?
Conversation
might be worth to define all error codes (in a new enum) to allow for easy future mapping of them to a different or more verbose message (like this pr) |
e5b899c
to
c2d5a70
Compare
I have added all the rpc error codes defined in bitcoin's protocol.h file |
The enum members don't need the and the enum definition and |
c2d5a70
to
fbaa2c3
Compare
@antonilol All done |
src/daemon.rs
Outdated
|
||
RpcForbiddenBySafeMode = -2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be removed as this was reserved in a version lower than the minimum supported bitcoind version of electrs
the documentation of the error codes can also be copied from the header file of bitcoind source |
Are you referring to the comments in the protocol.h file?
I'm not sure I understand what you mean here. Can I generate a from_i16 implementation on the enum with just declarative macros ( |
yes
yes with something along the lines of macro_rules! define_error_codes {
($($name:ident = $value:expr,)*) => {
/// some doc comments
// some derives
#[derive(Debug, Clone, Copy)]
#[repr(i16)]
pub enum CoreRPCErrorCode {
$(
$name = $value,
)*
}
impl CoreRPCErrorCode {
pub fn from_error_code(code: i16) -> Option<Self> {
Some(match code {
$(
$value => Self::$name,
)*
_ => return None,
})
}
}
};
}
define_error_codes! {
RpcInvalidRequest = -32600,
RpcMethodNotFound = -32601,
RpcInvalidParams = -32602,
RpcInternalError = -32603,
RpcParseError = -32700,
... etc
}
impl CoreRPCErrorCode {
pub fn to_error_code(self) -> i16 {
self as i16
}
} it can also be made more flexible but i dont think that is needed (at least yet), but you could also take the name and visibility of the enum as metavariables. |
c5dece3
to
2f4e29d
Compare
@antonilol Thanks for your help with the |
2f4e29d
to
bea3436
Compare
this is great! (test fails on a type mismatch, change the input type of from_error_code from i16 to i32 and should be fine) |
6f0674f
to
5da08b4
Compare
@antonilol I had to remove the |
@romanz Is there anything else I need to address before this can be merged? |
@@ -653,7 +664,10 @@ impl Call { | |||
.downcast_ref::<bitcoincore_rpc::Error>() | |||
.and_then(extract_bitcoind_error) | |||
{ | |||
Some(e) => error_msg(&self.id, RpcError::DaemonError(e.clone())), | |||
Some(e) => match self.params.parse_rpc_error_code(e.code) { | |||
Some(err) => error_msg(&self.id, err), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC this is sending the underlying error message to the caller. And while it's not recommended to connect untrusted devices to electrs some people may do that and it'd be better to just log the message and send back "internal server error" to avoid leaking information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC this is sending the underlying error message to the caller.
It is not. I added self.params.parse_rpc_error_code
to allow certain errors to be intercepted and replaced but the underlying error message is not sent to the caller
Defines all the rpc error codes specified in bitcoin's protocol.h file
5da08b4
to
48d55fa
Compare
This PR improves the error message returned for blockchain.transaction.get when the bitcoin daemon cannot find the transaction.
See #936