Sha256: 88ba4bfbda0178923b51bbc77e1c96903219596144d6fc8902f8a261561bc139

Contents?: true

Size: 1020 Bytes

Versions: 1

Compression:

Stored size: 1020 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

module Console
	# Whether the current fiber is emitting a warning.
	Fiber.attr_accessor :console_warn
	
	module Warn
		def warn(*arguments, uplevel: nil, **options)
			fiber = Fiber.current
			
			# We do this to be extra pendantic about avoiding infinite recursion, i.e. if `Console.warn` some how calls `Kernel.warn` again, it would potentially cause infinite recursion. I'm not sure if this is a problem in practice, but I'd rather not find out the hard way...
			return super if fiber.console_warn
			
			if uplevel
				options[:backtrace] = caller(uplevel, 1)
			end
			
			if arguments.last.is_a?(Exception)
				exception = arguments.pop
				
				Console::Event::Failure.for(exception).emit(*arguments, severity: :warn)
			else
				begin
					fiber.console_warn = true
					Console.warn(*arguments, **options)
				ensure
					fiber.console_warn = false
				end
			end
		end
	end
	
	::Kernel.prepend(Warn)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
console-1.28.1 lib/console/warn.rb