Skip to content
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

Improve error handling for database problems #517

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Nov 7, 2024

Closes #516

This change allows Ruby LSP to stay alive even if an error occurred when trying to connect to the database.

I also added a specific rescue for ActiveRecord::NoDatabaseError since this could be a common problem. Without that, users would see a very long stacktrace in the logs.

It may partially help with #521 too.

@andyw8 andyw8 added the bugfix This PR fixes an existing bug label Nov 7, 2024
@andyw8 andyw8 marked this pull request as ready for review November 7, 2024 15:42
@andyw8 andyw8 requested a review from a team as a code owner November 7, 2024 15:42
@andyw8 andyw8 force-pushed the andyw8/improve-error-handling-for-database-problems branch from 271cf38 to 44cf6a1 Compare November 7, 2024 16:14
@@ -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.

@andyw8
Copy link
Contributor Author

andyw8 commented Nov 7, 2024

Looking into adding tests for this.

@andyw8 andyw8 force-pushed the andyw8/improve-error-handling-for-database-problems branch from 44cf6a1 to df0c0c1 Compare November 7, 2024 16:26
@andyw8
Copy link
Contributor Author

andyw8 commented Nov 7, 2024

Added tests.

@andyw8 andyw8 enabled auto-merge (squash) November 7, 2024 16:30
@andyw8 andyw8 merged commit b5235c3 into main Nov 7, 2024
28 checks passed
@andyw8 andyw8 deleted the andyw8/improve-error-handling-for-database-problems branch November 7, 2024 16:37
@glennsarti
Copy link

Thanks for the quick turnaround! Really appreciated 🙇‍♂️

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Starting ruby-lsp-rails fails if database doesn't exist
4 participants