From: "Eregon (Benoit Daloze)" Date: 2021-09-29T10:10:01+00:00 Subject: [ruby-core:105486] [Ruby master Feature#18194] No easy way to format exception messages per thread/fiber scheduler context. Issue #18194 has been updated by Eregon (Benoit Daloze). ioquatix (Samuel Williams) wrote in #note-11: > I think having a top level hook for exceptions makes total sense. Maybe, but that won't be of any use for e.g. web applications which need to catch and format the exception before it kills the thread. The current default to print Exception#full_message for exceptions reaching the top of a thread seems already good enough to me. For formatting maybe full_message is already good enough? If the usage needs no colors it can say highlight: false, if it wants some it can indeed relatively easily parse them. I think it's already clear that Exception#message itself should not use ANSI escape sequences (for color/bold/etc). ---------------------------------------- Feature #18194: No easy way to format exception messages per thread/fiber scheduler context. https://bugs.ruby-lang.org/issues/18194#change-93935 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * Assignee: mame (Yusuke Endoh) ---------------------------------------- In the new error highlighting gem, formatting exception messages appears to be per-process which is insufficiently nuanced for existing use cases. As in: ```ruby class TerminalColorFormatter def message_for(spot) # How do we know the output format here? Maybe it's being written to a log file? "..." end end ErrorHighlight.formatter = TerminalColorFormatter.new ``` But we won't know until the time we actually write the error message whether terminal codes are suitable or available. Or an error message might be formatted for both the terminal and a log file, which have different formatting requirements. There are many consumers of error messages an some of them produce text, or HTML, or JSON, etc. Because of this design we are effectively forcing everyone to parse the default text output if they want to do any kind of formatting, which will ossify the format and make it impossible in practice for anyone to use anything but the default `ErrorHighlight.format`. For what is otherwise a really fantastic idea, this implementation concerns me greatly. I would like us to consider introducing sufficient metadata on the exception object so that complete formatting can be implemented by an output layer (e.g. logger, terminal wrapper, etc). This allows the output layer to intelligently format the output in a suitable way, or capture the metadata to allow for processing elsewhere. In addition, to simplify this general usage, we might like to introduce `Exception#formatted_message`. In order to handle default formatting requirements, we need to provide a hook for formatting uncaught exceptions. This would be excellent for many different use cases (e.g. HoneyBadger type systems), and I suggest we think about the best interface. Probably a thread-local with some default global implementation makes sense... maybe even something similar to `at_exit { ... $! ... }`. -- https://bugs.ruby-lang.org/ Unsubscribe: