Skip to content

Commit

Permalink
Add descriptive message for unexpected DSL exceptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
paracycle committed Apr 29, 2022
1 parent 518adff commit 58965e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/tapioca/dsl/pipeline.rb
Expand Up @@ -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?
Expand Down
36 changes: 36 additions & 0 deletions spec/tapioca/cli/dsl_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 58965e4

Please sign in to comment.