From 58965e40d126a365a91a716ae828ed3ac348f2b6 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Fri, 29 Apr 2022 15:55:37 +0300 Subject: [PATCH] Add descriptive message for unexpected DSL exceptions Currently, when a DSL compiler raises an error while running a `decorate` operation, it is not very obvious which compiler failed and during the processing of which constant. The failing DSL compiler is kind of obvious if one looks at the backtrace, but the constant that it is failing on is very hard to understand if one is not running DSL generation in verbose mode. This PR handles unexpected exceptions, shows a friendly error message for them and re-raises the exception to keep the current behaviour. --- lib/tapioca/dsl/pipeline.rb | 3 +++ spec/tapioca/cli/dsl_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/tapioca/dsl/pipeline.rb b/lib/tapioca/dsl/pipeline.rb index efe210c9f..502528e08 100644 --- a/lib/tapioca/dsl/pipeline.rb +++ b/lib/tapioca/dsl/pipeline.rb @@ -152,6 +152,9 @@ def rbi_for_constant(constant) compiler = compiler_class.new(self, file.root, constant) compiler.decorate + rescue + $stderr.puts("Error: `#{compiler_class.name}` failed to generate RBI for `#{constant}`") + raise # This is an unexpected error, so re-raise it end return if file.root.empty? diff --git a/spec/tapioca/cli/dsl_spec.rb b/spec/tapioca/cli/dsl_spec.rb index 966237282..0032d19a5 100644 --- a/spec/tapioca/cli/dsl_spec.rb +++ b/spec/tapioca/cli/dsl_spec.rb @@ -1148,6 +1148,42 @@ class Image assert_project_file_exist("sorbet/rbi/dsl/job.rbi") assert_project_file_exist("sorbet/rbi/dsl/image.rbi") end + + it "shows a helpful error message when unexpected errors occur" do + @project.write("lib/post.rb", <<~RB) + class Post + end + RB + + @project.write("lib/compilers/post_compiler_that_raises.rb", <<~RB) + require "post" + + class PostCompilerThatRaises < Tapioca::Dsl::Compiler + def decorate + raise "Some unexpected error happened" + end + + def self.gather_constants + [::Post] + end + end + RB + + result = @project.tapioca("dsl") + + assert_equal(<<~OUT, result.out) + Loading Rails application... Done + Loading DSL compiler classes... Done + Compiling DSL RBI files... + + OUT + + assert_includes(result.err, "Error: `PostCompilerThatRaises` failed to generate RBI for `Post`") + assert_includes(result.err, "Some unexpected error happened") + + refute_project_file_exist("sorbet/rbi/dsl/post.rbi") + refute_success_status(result) + end end describe "verify" do