From f2f1474fac5d88c4ad512a36119beb1a270769f2 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Sat, 17 Feb 2024 20:05:25 -0300 Subject: [PATCH] cli: add RescueMixin Add an error handler to the CLI --- twenty-cli/lib/twenty-cli/command.rb | 1 + twenty-cli/lib/twenty-cli/command/connect.rb | 1 + twenty-cli/lib/twenty-cli/command/console.rb | 1 + .../lib/twenty-cli/command/disconnect.rb | 1 + twenty-cli/lib/twenty-cli/command/down.rb | 5 ++-- twenty-cli/lib/twenty-cli/command/migrate.rb | 1 + .../twenty-cli/command/mixin/rescue_mixin.rb | 26 +++++++++++++++++++ twenty-cli/lib/twenty-cli/command/up.rb | 1 + twenty-cli/twenty-cli.gemspec | 1 + twenty-cli/twenty-cli.gemspec.erb | 1 + .../servlet/mixin/server_mixin.rb | 3 ++- 11 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 twenty-cli/lib/twenty-cli/command/mixin/rescue_mixin.rb diff --git a/twenty-cli/lib/twenty-cli/command.rb b/twenty-cli/lib/twenty-cli/command.rb index 0fe4dae..38477f1 100644 --- a/twenty-cli/lib/twenty-cli/command.rb +++ b/twenty-cli/lib/twenty-cli/command.rb @@ -8,6 +8,7 @@ class Twenty::Command < Cmd require_relative "command/mixin/common_option_mixin" require_relative "command/mixin/migration_mixin" require_relative "command/mixin/sqlite_mixin" + require_relative "command/mixin/rescue_mixin" ## # commands diff --git a/twenty-cli/lib/twenty-cli/command/connect.rb b/twenty-cli/lib/twenty-cli/command/connect.rb index 2c9fd69..0061c87 100644 --- a/twenty-cli/lib/twenty-cli/command/connect.rb +++ b/twenty-cli/lib/twenty-cli/command/connect.rb @@ -5,6 +5,7 @@ class Twenty::Command::Connect < Twenty::Command description: "Connect a project to twenty" prepend Twenty::Command::MigrationMixin prepend Twenty::Command::SQLiteMixin + prepend Twenty::Command::RescueMixin def run options = parse_options(argv) diff --git a/twenty-cli/lib/twenty-cli/command/console.rb b/twenty-cli/lib/twenty-cli/command/console.rb index 7c2196f..19108a0 100644 --- a/twenty-cli/lib/twenty-cli/command/console.rb +++ b/twenty-cli/lib/twenty-cli/command/console.rb @@ -6,6 +6,7 @@ class Twenty::Command::Console < Twenty::Command include CommonOptionMixin prepend Twenty::Command::MigrationMixin prepend Twenty::Command::SQLiteMixin + prepend Twenty::Command::RescueMixin def run options = parse_options(argv) diff --git a/twenty-cli/lib/twenty-cli/command/disconnect.rb b/twenty-cli/lib/twenty-cli/command/disconnect.rb index f935e9a..595daa4 100644 --- a/twenty-cli/lib/twenty-cli/command/disconnect.rb +++ b/twenty-cli/lib/twenty-cli/command/disconnect.rb @@ -5,6 +5,7 @@ class Twenty::Command::Disconnect < Twenty::Command description: "Disconnect a project from twenty" prepend Twenty::Command::MigrationMixin prepend Twenty::Command::SQLiteMixin + prepend Twenty::Command::RescueMixin def run options = parse_options(argv) diff --git a/twenty-cli/lib/twenty-cli/command/down.rb b/twenty-cli/lib/twenty-cli/command/down.rb index 33c992c..e3d0c61 100644 --- a/twenty-cli/lib/twenty-cli/command/down.rb +++ b/twenty-cli/lib/twenty-cli/command/down.rb @@ -5,6 +5,7 @@ class Twenty::Command::Down < Twenty::Command description: "Stop the twenty web server" include Twenty::Path prepend Twenty::Command::SQLiteMixin + prepend Twenty::Command::RescueMixin def run options = parse_options(argv) @@ -15,7 +16,7 @@ class Twenty::Command::Down < Twenty::Command def run_command(options) if File.readable?(pidfile) - Process.kill('SIGINT', Integer(pid)) + Process.kill("SIGINT", Integer(pid)) else warn "PID file is not readable." end @@ -27,6 +28,6 @@ class Twenty::Command::Down < Twenty::Command def pid @pid ||= File .binread(pidfile) - .gsub(/[^\d]/, '') + .gsub(/[^\d]/, "") end end diff --git a/twenty-cli/lib/twenty-cli/command/migrate.rb b/twenty-cli/lib/twenty-cli/command/migrate.rb index 64342f1..850e8d1 100644 --- a/twenty-cli/lib/twenty-cli/command/migrate.rb +++ b/twenty-cli/lib/twenty-cli/command/migrate.rb @@ -5,6 +5,7 @@ class Twenty::Command::Migrate < Twenty::Command description: "Migrate the database" include CommonOptionMixin prepend Twenty::Command::SQLiteMixin + prepend Twenty::Command::RescueMixin def run options = parse_options(argv) diff --git a/twenty-cli/lib/twenty-cli/command/mixin/rescue_mixin.rb b/twenty-cli/lib/twenty-cli/command/mixin/rescue_mixin.rb new file mode 100644 index 0000000..0f7b834 --- /dev/null +++ b/twenty-cli/lib/twenty-cli/command/mixin/rescue_mixin.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class Twenty::Command + module RescueMixin + def run(...) + super(...) + rescue => ex + require "paint" + $stderr.print "\n", + " ", Paint[" Exception ", :white, :red, :bold], "\n", + " ", Paint[ex.class.to_s, :bold], "\n", + " ", ex.message, "\n\n", + " ", Paint[" Backtrace ", :white, :blue, :bold], "\n", + format_backtrace(ex.backtrace), "\n", + "\n" + end + + private + + def format_backtrace(backtrace) + backtrace.last(5).map do + " #{_1.gsub(Dir.getwd, "")}" + end.join("\n") + end + end +end diff --git a/twenty-cli/lib/twenty-cli/command/up.rb b/twenty-cli/lib/twenty-cli/command/up.rb index 1e7f259..18200af 100644 --- a/twenty-cli/lib/twenty-cli/command/up.rb +++ b/twenty-cli/lib/twenty-cli/command/up.rb @@ -20,6 +20,7 @@ class Twenty::Command::Up < Twenty::Command include CommonOptionMixin prepend Twenty::Command::MigrationMixin prepend Twenty::Command::SQLiteMixin + prepend Twenty::Command::RescueMixin def run options = parse_options(argv) diff --git a/twenty-cli/twenty-cli.gemspec b/twenty-cli/twenty-cli.gemspec index 5b0e14c..3a4f8b2 100644 --- a/twenty-cli/twenty-cli.gemspec +++ b/twenty-cli/twenty-cli.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |gem| gem.description = gem.summary gem.executables = ["twenty", "20"] gem.add_runtime_dependency "cmd.rb", "~> 0.4" + gem.add_runtime_dependency "paint", "~> 2.3" gem.add_development_dependency "test-unit", "~> 3.5.7" gem.add_development_dependency "yard", "~> 0.9" gem.add_development_dependency "redcarpet", "~> 3.5" diff --git a/twenty-cli/twenty-cli.gemspec.erb b/twenty-cli/twenty-cli.gemspec.erb index bfaf717..5e6fe4c 100644 --- a/twenty-cli/twenty-cli.gemspec.erb +++ b/twenty-cli/twenty-cli.gemspec.erb @@ -18,6 +18,7 @@ Gem::Specification.new do |gem| gem.description = gem.summary gem.executables = ["twenty", "20"] gem.add_runtime_dependency "cmd.rb", "~> 0.4" + gem.add_runtime_dependency "paint", "~> 2.3" gem.add_development_dependency "test-unit", "~> 3.5.7" gem.add_development_dependency "yard", "~> 0.9" gem.add_development_dependency "redcarpet", "~> 3.5" diff --git a/twenty-server/lib/twenty-server/servlet/mixin/server_mixin.rb b/twenty-server/lib/twenty-server/servlet/mixin/server_mixin.rb index 8462056..79dc566 100644 --- a/twenty-server/lib/twenty-server/servlet/mixin/server_mixin.rb +++ b/twenty-server/lib/twenty-server/servlet/mixin/server_mixin.rb @@ -39,7 +39,8 @@ module Twenty::Servlet::ServerMixin { DocumentRoot: Twenty.build, BindAddress: cli_options.bind, - Port: cli_options.port + Port: cli_options.port, + Logger: WEBrick::Log.new(File::NULL) } end end