Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Should recovering the type_name of a dyn Error be supported? #52

Open
@thomcc

Description

@thomcc

It would be nice (IMO) if there were a way to recover the type name (as produced by core::any::type_name1) of a dyn Error. I've wanted this several times, as in many cases, it can be useful as a summary of what went wrong at a fairly high level, and without any details (since the details are available through other means, such as the Debug/Display implementations).

(A concrete use case I've recently hit involves serializing Error instances for remote reporting, where the type name could be useful for broad categorization. That said, for my case it is not a problem to capture this information when the error is generated, before its type is erased. So... I don't know how compelling this example is)

On the other hand, perhaps this isn't worth having. In principal, enough information to figure out what type the error initially had should probably be present in the Debug output. And it may be too similar to Error::description(), which we deprecated -- presumably (I don't actually know the reason) for being inflexible and redundant, which are arguably flaws this would have as well. Also, the fact that I was able to capture this prior to type erasure without much pain indicates that perhaps this would be true for most other situations where it's needed.

If we do want it, I think it would be straightforward to implement -- I'm imagining that this would just be a type_name(&self) -> &'static str on the error trait, and would have an implementation similar to that of Error::type_id. I think it would have the same non-guarantees that core::any::type_name has for consistency, accuracy, etc. (Also, while I don't see a reason to allow users to override it, I don't really see a reason to forbid it either -- It's not like anybody can trust it being accurate or consistent, after all). That said, I don't feel very strongly about these implementation details -- you certainly could design it other ways (including repurposing Error::description(), although this would certainly not be my suggestion).

(P.S. Sorry if this has been discussed, I didn't see it)

Footnotes

  1. Note that while core::any::type_name_of_val exists and seems like it would support trait objects, it doesn't (at least, not really -- it essentially just reports dyn std::error::Error). I believe this is intentional, and that it probably will due to the nontrivial binary size cost this would incur, even for code that doesn't use it.

    This is discussed in the type_name_of_val tracking issue, and despite my desire to have type names work for Error instances, I don't think I would be in favor of it work on all trait objects; due to said cost.

    That said, for errors it makes sense to me, for the reason mentioned above (it can be a useful high-level summary of what went wrong).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions