Skip to content

add From<i32> impl for GrpcCode#506

Merged
jlizen merged 2 commits intotower-rs:mainfrom
gshipilov:grpc-code-from-i32
May 6, 2026
Merged

add From<i32> impl for GrpcCode#506
jlizen merged 2 commits intotower-rs:mainfrom
gshipilov:grpc-code-from-i32

Conversation

@gshipilov
Copy link
Copy Markdown
Contributor

This change adds a From<i32> impl for the GrpcCode enum. This allows users to easily convert the result of the GrpcErrorsAsFailures back into an enum.

Fixes: #505

Motivation

I recently had to implement a custom TraceLayer on-failure struct, and it made sense to reuse the GrpcErrorsAsFailures class to extract the status. Unfortunately, it returns the error code as a NoneZeroI32 but tower_http doesn't have a convenient way to map this back to the num.

Implementing this is trivial, but still feels like a reasonable addition to this library.

Solution

I feel like this solution is pretty self-explanatory :).

Copy link
Copy Markdown
Member

@jlizen jlizen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me! It matches tonic's approach:

(including the lossy fallback to Unknown)

Let me throw in a quick test and comment, and get this merged.

@jlizen jlizen enabled auto-merge (squash) May 6, 2026 23:56
@jlizen jlizen merged commit 991e9ee into tower-rs:main May 6, 2026
12 checks 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.

Implement From<NonZeroI32> for GrpcCode

2 participants