Skip to content

Commit

Permalink
Use different error code for code action errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Sep 30, 2024
1 parent 88785b7 commit 8f13d13
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
6 changes: 6 additions & 0 deletions lib/ruby_lsp/base_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def process_message(message); end
sig { abstract.void }
def shutdown; end

sig { params(id: Integer, message: String).void }
def fail_request_and_notify(id, message)
send_message(Error.new(id: id, code: Constant::ErrorCodes::REQUEST_FAILED, message: message))
send_message(Notification.window_show_message(message, type: Constant::MessageType::WARNING))
end

sig { returns(Thread) }
def new_worker
Thread.new do
Expand Down
35 changes: 12 additions & 23 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def run_initialized
rescue RuboCop::Error => e
# The user may have provided unknown config switches in .rubocop or
# is trying to load a non-existant config file.
send_message(Notification.window_show_error(
send_message(Notification.window_show_message(
"RuboCop configuration error: #{e.message}. Formatting will not be available.",
))
end
Expand Down Expand Up @@ -531,10 +531,10 @@ def text_document_formatting(message)
response = Requests::Formatting.new(@global_state, document).perform
send_message(Result.new(id: message[:id], response: response))
rescue Requests::Request::InvalidFormatter => error
send_message(Notification.window_show_error("Configuration error: #{error.message}"))
send_message(Notification.window_show_message("Configuration error: #{error.message}"))
send_empty_response(message[:id])
rescue StandardError, LoadError => error
send_message(Notification.window_show_error("Formatting error: #{error.message}"))
send_message(Notification.window_show_message("Formatting error: #{error.message}"))
send_empty_response(message[:id])
end

Expand Down Expand Up @@ -673,30 +673,19 @@ def code_action_resolve(message)
document = @store.get(uri)

unless document.is_a?(RubyDocument)
send_message(Notification.window_show_error("Code actions are currently only available for Ruby documents"))
raise Requests::CodeActionResolve::CodeActionError
fail_request_and_notify(message[:id], "Code actions are currently only available for Ruby documents")
return
end

result = Requests::CodeActionResolve.new(document, params).perform

case result
when Requests::CodeActionResolve::Error::EmptySelection
send_message(Notification.window_show_error("Invalid selection for Extract Variable refactor"))
raise Requests::CodeActionResolve::CodeActionError
fail_request_and_notify(message[:id], "Invalid selection for extract variable refactor")
when Requests::CodeActionResolve::Error::InvalidTargetRange
send_message(
Notification.window_show_error(
"Couldn't find an appropriate location to place extracted refactor",
),
)
raise Requests::CodeActionResolve::CodeActionError
fail_request_and_notify(message[:id], "Couldn't find an appropriate location to place extracted refactor")
when Requests::CodeActionResolve::Error::UnknownCodeAction
send_message(
Notification.window_show_error(
"Unknown code action",
),
)
raise Requests::CodeActionResolve::CodeActionError
fail_request_and_notify(message[:id], "Unknown code action")
else
send_message(Result.new(id: message[:id], response: result))
end
Expand Down Expand Up @@ -729,10 +718,10 @@ def text_document_diagnostic(message)
),
)
rescue Requests::Request::InvalidFormatter => error
send_message(Notification.window_show_error("Configuration error: #{error.message}"))
send_message(Notification.window_show_message("Configuration error: #{error.message}"))
send_empty_response(message[:id])
rescue StandardError, LoadError => error
send_message(Notification.window_show_error("Error running diagnostics: #{error.message}"))
send_message(Notification.window_show_message("Error running diagnostics: #{error.message}"))
send_empty_response(message[:id])
end

Expand Down Expand Up @@ -969,7 +958,7 @@ def perform_initial_indexing
false
end
rescue StandardError => error
send_message(Notification.window_show_error("Error while indexing: #{error.message}"))
send_message(Notification.window_show_message("Error while indexing: #{error.message}"))
end

# Indexing produces a high number of short lived object allocations. That might lead to some fragmentation and
Expand Down Expand Up @@ -1054,7 +1043,7 @@ def check_formatter_is_available
@global_state.formatter = "none"

send_message(
Notification.window_show_error(
Notification.window_show_message(
"Ruby LSP formatter is set to `rubocop` but RuboCop was not found in the Gemfile or gemspec.",
),
)
Expand Down
9 changes: 3 additions & 6 deletions lib/ruby_lsp/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ class Notification < Message
class << self
extend T::Sig

sig { params(message: String).returns(Notification) }
def window_show_error(message)
sig { params(message: String, type: Integer).returns(Notification) }
def window_show_message(message, type: Constant::MessageType::ERROR)
new(
method: "window/showMessage",
params: Interface::ShowMessageParams.new(
type: Constant::MessageType::ERROR,
message: message,
),
params: Interface::ShowMessageParams.new(type: type, message: message),
)
end

Expand Down
9 changes: 8 additions & 1 deletion vscode/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,14 @@ export default class Client extends LanguageClient implements ClientInterface {
} else {
const { errorMessage, errorClass, backtrace } = error.data;

if (errorMessage && errorClass && backtrace) {
// We only want to produce telemetry events for errors that have all the data we need and that are internal
// server errors. Other errors do not necessarily indicate bugs in the server
if (
errorMessage &&
errorClass &&
backtrace &&
error.code === -32603
) {
// Sanitize the backtrace coming from the server to remove the user's home directory from it, then mark it
// as a trusted value. Otherwise the VS Code telemetry logger redacts the entire backtrace and we are unable
// to see where in the server the error occurred
Expand Down

0 comments on commit 8f13d13

Please sign in to comment.