GEN-159: Local `install_debug_hook`
Related Problem
Report::install_debug_hook / Report::set_color_mode / Report::set_charset seems all global settings.
I'd prefer to switch the manner locally. For example, I'd switch the manner when returning HTTP error response to trim the Backtrace with Report::install_debug_hook::<std::backtrace::Backtrace>(|_, _| {}); as well as use ASCII and no color, but I'm OK with these settings in all other situations.
Proposed Solution
No idea. I'm not quite sure about the philosophy.
Alternatives
No response
Additional context
No response
Hi @tisonkun.
Yes, the mentioned functions are global. Do you have an idea how we could achieve the behavior? If I understand correctly you want to attach more information to the Report how this should be formatted?
Maybe, it's possible to add a function which internally adds formatting hooks to one report and use the global functions as fallback, what do you think @indietyp?
That should be possible. Yes. There might be some hurdles in implementing this in a way that isn't overcomplicating things, but in theory: Yes.
Do you have an idea how we could achieve the behavior
I'm not quite familiar with the codebase and don't take a closer look yet.
But here are two major considerations:
- It's generally possible to use a fallback strategy for preferring a local debug hook while falling back to the global one when the local one is
None. - The reason we need such a global setting, IIUC, is that
{:?}hard code to callDebug::fmtand we don't have a place to pass the hook config. However, in my situation or anyone who wants to alter the manner locally, I'm OK to call a different method, likeReport::debug_fmt(&self, debug_hook_config)to render the string.
Combine 1 and 2 may be a way to the solution. Hopefully this helps :D
After a closer look, I can see that here come two different issues:
The color mode and charset can be passed manually, by creating a Report::debug_fmt(&self, debug_hook_config) and delegate the current debug impl to it. When we'd like a local config, use the new debug_fmt instead.
The hook and context can be much more complex. But basically, print Backtrace is too large, so if we have a feature flag to turn it off, that should be enough. I think people would always want location, but not backtrace.
That said, we generally respect user-installed hooks, but the current always install backtrace built-in hook too noisy in some case.
cc @TimDiekmann @indietyp
@indietyp and I discussed how this could be implemented and we currently figuring out what the best API would be. Most likely it will be possible to simply .attach() some debug hook. On the nightly channel, it will probably also be possible to use Error::provide to inject debug hooks. That way, it is possible for a specific error (e.g. a user-facing API endpoint error) to provide some custom debug hooks like disabling the Backtrace.
But basically, print Backtrace is too large, so if we have a feature flag to turn it off, that should be enough.
Having a dedicated feature flag for Backtraces was requested in https://github.com/hashintel/hash/issues/3353 and was implemented in https://github.com/hashintel/hash/pull/4685.