Skip to content

Improve error handling for database problems #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def send_message(message)
# Log a message to the editor's output panel
def log_message(message)
$stderr.puts(message)
send_message({ result: nil })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed the right solution as we need to return something even when errors occur. But this is not the right place to put it.

The goal of the log_message method is to allow both the server and its add-ons to send debug messages to the output tab, even if disconnected from providing a result.

If a database error was to occur in a notification, where no response is expected, then we'd be writing a nil result to the pipe, which wouldn't be read by the notification. The next request would then incorrectly read it and we'd end up with the request/response cycle out of sync.

We need to move this line to where error handling happens, so that log_message is not coupled with returning responses to the editor. And this result can only be returned when rescuing errors for requests - not for notifications.

Copy link
Contributor Author

@andyw8 andyw8 Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I wasn't considering usage from add-ons.

#523

end
end

Expand Down Expand Up @@ -103,6 +104,9 @@ def start
end

def execute(request, params)
request_name = request
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in the wrong place previously.

request_name = "#{params[:server_addon_name]}##{params[:request_name]}" if request == "server_addon/delegate"

case request
when "shutdown"
@running = false
Expand All @@ -128,11 +132,11 @@ def execute(request, params)
request_name = params[:request_name]
ServerAddon.delegate(server_addon_name, request_name, params.except(:request_name, :server_addon_name))
end
request_name = request
request_name = "#{params[:server_addon_name]}##{params[:request_name]}" if request == "server_addon/delegate"
# Since this is a common problem, we show a specific error message to the user, instead of the full stack trace.
rescue ActiveRecord::ConnectionNotEstablished
log_message("Request #{request_name} failed because database connection was not established.")
rescue ActiveRecord::NoDatabaseError
log_message("Request #{request_name} failed because the database does not exist.")
rescue => e
log_message("Request #{request_name} failed:\n" + e.full_message(highlight: false))
end
Expand Down
20 changes: 20 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ def resolve_route_info(requirements)
assert_equal({ message: "Running migrations...", status: 0 }, response[:result])
end

test "shows error if there is a database connection error" do
@server.expects(:pending_migrations_message).raises(ActiveRecord::ConnectionNotEstablished)

_, stderr = capture_subprocess_io do
@server.execute("pending_migrations_message", {})
end
assert_equal(stderr, "Request pending_migrations_message failed because database connection was not established.\n")
assert_equal({ result: nil }, response)
end

test "shows error if database does not exist" do
@server.expects(:pending_migrations_message).raises(ActiveRecord::NoDatabaseError)

_, stderr = capture_subprocess_io do
@server.execute("pending_migrations_message", {})
end
assert_equal(stderr, "Request pending_migrations_message failed because the database does not exist.\n")
assert_equal({ result: nil }, response)
end

private

def response
Expand Down
Loading