Skip to content

Commit

Permalink
(maint) Do not error in validation exception handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glennsarti committed Nov 18, 2017
1 parent 8d0ef40 commit b1515ea
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/puppet-languageserver/document_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,20 @@ def self.validate(content, workspace, _max_problems = 100)
validation_environment.check_for_reparse
validation_environment.known_resource_types.clear
rescue StandardError => detail
# Somtimes the error is in the cause not the root object itself
detail = detail.cause if !detail.respond_to?(:line) && detail.respond_to?(:cause) && detail.cause.respond_to?(:line)

# Sometimes the error is in the cause not the root object itself
detail = detail.cause if !detail.respond_to?(:line) && detail.respond_to?(:cause)
ex_line = detail.respond_to?(:line) && !detail.line.nil? ? detail.line - 1 : nil # Line numbers from puppet exceptions are base 1
ex_pos = detail.respond_to?(:pos) && !detail.pos.nil? ? detail.pos : nil # Pos numbers from puppet are base 1

message = detail.respond_to?(:message) ? detail.message : nil
message = detail.basic_message if message.nil? && detail.respond_to?(:basic_message)

unless detail.line.nil? || detail.pos.nil? || message.nil?
unless ex_line.nil? || ex_pos.nil? || message.nil?
result << LanguageServer::Diagnostic.create('severity' => LanguageServer::DIAGNOSTICSEVERITY_ERROR,
'fromline' => detail.line - 1, # Line numbers from puppet are base 1
'toline' => detail.line - 1, # Line numbers from puppet are base 1
'fromchar' => detail.pos - 1, # Pos numbers from puppet are base 1
'tochar' => detail.pos + 1 - 1, # Pos numbers from puppet are base 1
'fromline' => ex_line,
'toline' => ex_line,
'fromchar' => ex_pos,
'tochar' => ex_pos + 1,
'source' => 'Puppet',
'message' => message)
end
Expand Down

0 comments on commit b1515ea

Please sign in to comment.