From 4a7549e159d39b654de9a17ce732bf6ae456decd Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 6 Nov 2024 15:17:54 +1300 Subject: [PATCH] Use simpler interface for emitting warnings, and add shim for Console gem. --- async.gemspec | 2 +- lib/async/console.rb | 37 +++++++++++++++++++++++++++++++++++++ lib/async/task.rb | 6 ++---- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 lib/async/console.rb diff --git a/async.gemspec b/async.gemspec index 7cd8f0e2..5fe97dfd 100644 --- a/async.gemspec +++ b/async.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 3.1" - spec.add_dependency "console", "~> 1.26" + spec.add_dependency "console", "~> 1.29" spec.add_dependency "fiber-annotation" spec.add_dependency "io-event", ["~> 1.6", ">= 1.6.5"] end diff --git a/lib/async/console.rb b/lib/async/console.rb new file mode 100644 index 00000000..99295ec8 --- /dev/null +++ b/lib/async/console.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2024, by Samuel Williams. + +module Async + # Shims for the console gem, redirecting warnings and above to `Kernel#warn`. + # + # If you require this file, the `async` library will not depend on the `console` gem. + # + # That includes any gems that sit within the `Async` namespace. + # + # This is an experimental feature. + module Console + def self.debug(...) + end + + def self.info(...) + end + + def self.warn(*arguments, exception: nil, **options) + if exception + super(*arguments, exception.full_message, **options) + else + super(*arguments, **options) + end + end + + def self.error(...) + self.warn(...) + end + + def self.fatal(...) + self.warn(...) + end + end +end diff --git a/lib/async/task.rb b/lib/async/task.rb index 31c348d4..68e8f6fa 100644 --- a/lib/async/task.rb +++ b/lib/async/task.rb @@ -8,7 +8,7 @@ # Copyright, 2023, by Math Ieu. require "fiber" -require "console/event/failure" +require "console" require_relative "node" require_relative "condition" @@ -198,9 +198,7 @@ def run(*arguments) rescue => error # I'm not completely happy with this overhead, but the alternative is to not log anything which makes debugging extremely difficult. Maybe we can introduce a debug wrapper which adds extra logging. if @finished.nil? - Console::Event::Failure.for(error).emit(self, "Task may have ended with unhandled exception.", severity: :warn) - else - # Console::Event::Failure.for(error).emit(self, severity: :debug) + Console.warn(self, "Task may have ended with unhandled exception.", exception: error) end raise