Skip to content

Commit 9796037

Browse files
committed
(maint) Do not error in validation exception handler
Previosuly if an unexpected type of error came through the document validator it would itself raise a new exception and crash the Language Server. This was caused by the lack of a :line or :pos method on the exception or the inner exception. This commit changes the detection logic to ignore any exception that does not contain line and position information in the exception object. These can be ignored as they are not Puppet manifest errors but a symptom of something else.
1 parent 55cc029 commit 9796037

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

server/lib/puppet-languageserver/document_validator.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,20 @@ def self.validate(content, workspace, _max_problems = 100)
8888
validation_environment.check_for_reparse
8989
validation_environment.known_resource_types.clear
9090
rescue StandardError => detail
91-
# Somtimes the error is in the cause not the root object itself
92-
detail = detail.cause if !detail.respond_to?(:line) && detail.respond_to?(:cause) && detail.cause.respond_to?(:line)
93-
91+
# Sometimes the error is in the cause not the root object itself
92+
detail = detail.cause if !detail.respond_to?(:line) && detail.respond_to?(:cause)
93+
ex_line = detail.respond_to?(:line) && !detail.line.nil? ? detail.line - 1 : nil # Line numbers from puppet exceptions are base 1
94+
ex_pos = detail.respond_to?(:pos) && !detail.pos.nil? ? detail.pos : nil # Pos numbers from puppet are base 1
95+
9496
message = detail.respond_to?(:message) ? detail.message : nil
9597
message = detail.basic_message if message.nil? && detail.respond_to?(:basic_message)
9698

97-
unless detail.line.nil? || detail.pos.nil? || message.nil?
99+
unless ex_line.nil? || ex_pos.nil? || message.nil?
98100
result << LanguageServer::Diagnostic.create('severity' => LanguageServer::DIAGNOSTICSEVERITY_ERROR,
99-
'fromline' => detail.line - 1, # Line numbers from puppet are base 1
100-
'toline' => detail.line - 1, # Line numbers from puppet are base 1
101-
'fromchar' => detail.pos - 1, # Pos numbers from puppet are base 1
102-
'tochar' => detail.pos + 1 - 1, # Pos numbers from puppet are base 1
101+
'fromline' => ex_line,
102+
'toline' => ex_line,
103+
'fromchar' => ex_pos,
104+
'tochar' => ex_pos + 1,
103105
'source' => 'Puppet',
104106
'message' => message)
105107
end

0 commit comments

Comments
 (0)