From b1515ea9359c17f8fb8b2d38f6ce355b43bf6605 Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Fri, 17 Nov 2017 16:35:22 -0800 Subject: [PATCH] (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. --- .../document_validator.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/puppet-languageserver/document_validator.rb b/lib/puppet-languageserver/document_validator.rb index 928c7a07..2ed025e0 100644 --- a/lib/puppet-languageserver/document_validator.rb +++ b/lib/puppet-languageserver/document_validator.rb @@ -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