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

use non-static strings errors #16

Merged
merged 2 commits into from
Jan 1, 2025

Conversation

PiRK
Copy link
Contributor

@PiRK PiRK commented Dec 30, 2024

This enables uses cases that require error message built with runtime data such as

RPCError::InvalidParamsString(format!("Method {method_name} requires {num_required} params, got {num_actual}"))
// Forward error message
RPCError::CustomErrorString(1, err.what().to_string())

Here is an example of a use case:

https://reviews.bitcoinabc.org/D17427
In this example the server needs to forward the exact same error message returned by a backend bitcoin node when attempting to broadcast a transaction, so that clients can troubleshoot issues with transaction building.

@PiRK
Copy link
Contributor Author

PiRK commented Dec 30, 2024

I added new error types rather than changing the existing ones, to avoid breaking code for your other users. But I'm also happy with just changing the string type for the existing CustomError and InvalidParams if you prefer, and adjust the rest of my code accordingly.

I'm also open to suggestions for better names for these two new errors.

@hozan23
Copy link
Member

hozan23 commented Dec 30, 2024

Hello @PiRK,

Thank you for the PR. Could you please remove the ones with &str and use String instead? One of them is probably enough.

Something like:

#[error("Custom Error:  code: {0} msg: {1}")]
CustomError(i32, String),

#[error("Invalid Params: {0}")]
InvalidParams(String),

@hozan23
Copy link
Member

hozan23 commented Dec 30, 2024

Since you're already working on it, could you please change the other error types to use String instead of &'static str? I don't remember exactly why I chose &str instead of just String.

Thank you

@hozan23
Copy link
Member

hozan23 commented Dec 30, 2024

I added new error types rather than changing the existing ones, to avoid breaking code for your other users. But I'm also happy with just changing the string type for the existing CustomError and InvalidParams if you prefer, and adjust the rest of my code accordingly.

I'm also open to suggestions for better names for these two new errors.

I think it would be much cleaner to change the existing ones. Thanks.

This enables uses cases that require serving error messages built with runtime data such as
```
RPCError::InvalidParamsString(format!("Method {method_name} requires {num_required} params, got {num_actual}"))
```

```
// Forward error message
RPCError::CustomErrorString(1, err.what().to_string())
```
@PiRK PiRK force-pushed the custom_runtime_errors branch from 22eedd7 to da2a2b6 Compare December 30, 2024 12:20
@PiRK
Copy link
Contributor Author

PiRK commented Dec 30, 2024

I split the changes into two commits, one for RPCError (which does not require other changes in the codebase) and another one for other error types.

@PiRK PiRK changed the title add a non-static string variant for CustomError and InvalidParams use non-static strings errors Dec 30, 2024
@PiRK PiRK force-pushed the custom_runtime_errors branch from da2a2b6 to 7529856 Compare December 30, 2024 12:39
@hozan23 hozan23 merged commit c019ca3 into karyontech:master Jan 1, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants