| 
									
										
										
										
											2024-04-28 02:36:19 -04:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Utils | 
					
						
							|  |  |  |   module Backtrace | 
					
						
							| 
									
										
										
										
											2024-04-28 02:36:19 -04:00
										 |  |  |     @print_backtrace_message = T.let(false, T::Boolean) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  |     # Cleans `sorbet-runtime` gem paths from the backtrace unless... | 
					
						
							|  |  |  |     # 1. `verbose` is set | 
					
						
							|  |  |  |     # 2. first backtrace line starts with `sorbet-runtime` | 
					
						
							|  |  |  |     #   - This implies that the error is related to Sorbet. | 
					
						
							|  |  |  |     sig { params(error: Exception).returns(T.nilable(T::Array[String])) } | 
					
						
							|  |  |  |     def self.clean(error) | 
					
						
							|  |  |  |       backtrace = error.backtrace | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return backtrace if Context.current.verbose? | 
					
						
							|  |  |  |       return backtrace if backtrace.blank? | 
					
						
							|  |  |  |       return backtrace if backtrace.fetch(0).start_with?(sorbet_runtime_path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       old_backtrace_length = backtrace.length | 
					
						
							|  |  |  |       backtrace.reject { |line| line.start_with?(sorbet_runtime_path) } | 
					
						
							|  |  |  |                .tap { |new_backtrace| print_backtrace_message if old_backtrace_length > new_backtrace.length } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 16:35:10 +01:00
										 |  |  |     sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  |     def self.sorbet_runtime_path | 
					
						
							| 
									
										
										
										
											2024-04-28 02:36:19 -04:00
										 |  |  |       @sorbet_runtime_path ||= T.let("#{Gem.paths.home}/gems/sorbet-runtime", T.nilable(String)) | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 16:35:10 +01:00
										 |  |  |     sig { void } | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  |     def self.print_backtrace_message | 
					
						
							|  |  |  |       return if @print_backtrace_message | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       opoo "Removed Sorbet lines from backtrace!" | 
					
						
							| 
									
										
										
										
											2024-02-04 15:19:29 +01:00
										 |  |  |       puts "Rerun with `--verbose` to see the original backtrace" unless Homebrew::EnvConfig.no_env_hints? | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       @print_backtrace_message = true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-04-02 16:35:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sig { params(error: Exception).returns(T.nilable(String)) } | 
					
						
							|  |  |  |     def self.tap_error_url(error) | 
					
						
							|  |  |  |       backtrace = error.backtrace | 
					
						
							|  |  |  |       return if backtrace.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       backtrace.each do |line| | 
					
						
							|  |  |  |         if (tap = line.match(%r{/Library/Taps/([^/]+/[^/]+)/})) | 
					
						
							|  |  |  |           return "https://github.com/#{tap[1]}/issues/new" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       nil | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-09-11 21:54:27 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | end |