Skip to content

Commit

Permalink
Wrap extensions with try/catch and log exceptions
Browse files Browse the repository at this point in the history
Fixes eclipse#946

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 16, 2020
1 parent e2f31b8 commit 2bfb29a
Show file tree
Hide file tree
Showing 14 changed files with 751 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public List<CodeAction> doCodeActions(CodeActionContext context, Range range, DO
codeActionParticipant.doCodeAction(diagnostic, range, document, codeActions, sharedSettings,
extensionsRegistry);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while processing code action participant '"
+ codeActionParticipant.getClass().getName() + "'", e);
LOGGER.log(Level.SEVERE, "Error while processing code actions for the participant '"
+ codeActionParticipant.getClass().getName() + "'.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
Expand All @@ -31,6 +34,8 @@ class XMLCodeLens {

private final XMLExtensionsRegistry extensionsRegistry;

private static final Logger LOGGER = Logger.getLogger(XMLCodeLens.class.getName());

public XMLCodeLens(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}
Expand All @@ -39,7 +44,14 @@ public List<? extends CodeLens> getCodelens(DOMDocument xmlDocument, XMLCodeLens
ICodeLensRequest request = new CodeLensRequest(xmlDocument, settings);
List<CodeLens> lenses = new ArrayList<>();
for (ICodeLensParticipant participant : extensionsRegistry.getCodeLensParticipants()) {
participant.doCodeLens(request, lenses, cancelChecker);
try {
participant.doCodeLens(request, lenses, cancelChecker);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing code lens for the participant '" + participant.getClass().getName() + "'.", e);
}
}
return lenses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public CompletionList doComplete(DOMDocument xmlDocument, Position position, Sha

/**
* Collect snippets suggestions.
*
*
* @param completionRequest completion request.
* @param completionResponse completion response.
*/
Expand Down Expand Up @@ -370,7 +370,7 @@ private static Integer getSuffixIndex(String text, String suffix, final int init
/**
* Returns the limit start offset of the expression according to the current
* node.
*
*
* @param currentNode the node.
* @param offset the offset.
* @return the limit start offset of the expression according to the current
Expand Down Expand Up @@ -436,7 +436,7 @@ private static int getExprStart(String value, int from, int to) {
/**
* Returns true if completion was triggered inside DTD content (internal or
* external DTD) and false otherwise.
*
*
* @param node
* @param xmlDocument
* @return true if completion was triggered inside DTD content (internal or
Expand Down Expand Up @@ -627,7 +627,7 @@ private void collectOpenTagSuggestions(boolean hasOpenBracket, Range replaceRang
try {
participant.onTagOpen(completionRequest, completionResponse);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing ICompletionParticipant#onTagOpen", e);
LOGGER.log(Level.SEVERE, "While performing ICompletionParticipant#onTagOpen for participant '" + participant.getClass().getName() + "'.", e);
}
}
DOMElement parentNode = completionRequest.getParentElement();
Expand Down Expand Up @@ -763,7 +763,8 @@ private void collectInsideContent(CompletionRequest request, CompletionResponse
try {
participant.onXMLContent(request, response);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing ICompletionParticipant#onXMLContent", e);
LOGGER.log(Level.SEVERE, "While performing ICompletionParticipant#onXMLContent for participant '"
+ participant.getClass().getName() + "'.", e);
}
}
collectionRegionProposals(request, response);
Expand Down Expand Up @@ -855,7 +856,14 @@ private void collectAttributeNameSuggestions(int nameStart, int nameEnd, Complet
boolean generateValue = !isFollowedBy(text, nameEnd, ScannerState.AfterAttributeName,
TokenType.DelimiterAssign);
for (ICompletionParticipant participant : getCompletionParticipants()) {
participant.onAttributeName(generateValue, completionRequest, completionResponse);
try {
participant.onAttributeName(generateValue, completionRequest, completionResponse);
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"While performing ICompletionParticipant#onAttributeName for participant '"
+ participant.getClass().getName() + "'.",
e);
}
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "While performing Completions, getReplaceRange() was given a bad Offset location",
Expand Down Expand Up @@ -899,7 +907,14 @@ private void collectAttributeValueSuggestions(int valueStart, int valueEnd, Comp
completionRequest.setReplaceRange(replaceRange);
completionRequest.setAddQuotes(addQuotes);
for (ICompletionParticipant participant : completionParticipants) {
participant.onAttributeValue(valuePrefix, completionRequest, completionResponse);
try {
participant.onAttributeValue(valuePrefix, completionRequest, completionResponse);
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"While performing ICompletionParticipant#onAttributeValue for participant '"
+ participant.getClass().getName() + "'.",
e);
}
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE,
Expand All @@ -912,7 +927,7 @@ private void collectAttributeValueSuggestions(int valueStart, int valueEnd, Comp

/**
* Collect completion items for DTD systemId
*
*
* @param valueStart the start offset of the systemId value, including
* quote
* @param valueEnd the end offset of the systemId value, including
Expand All @@ -936,7 +951,14 @@ private void collectDTDSystemIdSuggestions(int valueStart, int valueEnd, Complet
Range replaceRange = getReplaceRange(valueContentStart, valueContentEnd, completionRequest);
completionRequest.setReplaceRange(replaceRange);
for (ICompletionParticipant participant : completionParticipants) {
participant.onDTDSystemId(valuePrefix, completionRequest, completionResponse);
try {
participant.onDTDSystemId(valuePrefix, completionRequest, completionResponse);
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"While performing ICompletionParticipant#onDTDSystemId for participant '"
+ participant.getClass().getName() + "'.",
e);
}
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE,
Expand All @@ -959,7 +981,7 @@ private static int scanNextForEndPos(int offset, Scanner scanner, TokenType next

/**
* Returns list of {@link ICompletionParticipant}.
*
*
* @return list of {@link ICompletionParticipant}.
*/
private Collection<ICompletionParticipant> getCompletionParticipants() {
Expand All @@ -973,7 +995,7 @@ private static boolean isFollowedBy(String s, int offset, ScannerState intialSta
/**
* Returns starting offset of 'expectedToken' if it the next non whitespace
* token after 'initialState'
*
*
* @param s
* @param offset
* @param intialState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -57,7 +58,14 @@ public List<? extends LocationLink> findDefinition(DOMDocument document, Positio
// Custom definition
List<LocationLink> locations = new ArrayList<>();
for (IDefinitionParticipant participant : extensionsRegistry.getDefinitionParticipants()) {
participant.findDefinition(request, locations, cancelChecker);
try {
participant.findDefinition(request, locations, cancelChecker);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing definitions for the participant '" + participant.getClass().getName() + "'.", e);
}
}
// Start end tag definition
findStartEndTagDefinition(request, locations);
Expand All @@ -66,7 +74,7 @@ public List<? extends LocationLink> findDefinition(DOMDocument document, Positio

/**
* Find start end tag definition.
*
*
* @param request the definition request
* @param locations the locations
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.uriresolver.CacheResourceDownloadingException;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

Expand All @@ -29,6 +33,7 @@
class XMLDiagnostics {

private final XMLExtensionsRegistry extensionsRegistry;
private static final Logger LOGGER = Logger.getLogger(XMLDiagnostics.class.getName());

public XMLDiagnostics(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
Expand All @@ -45,7 +50,7 @@ public List<Diagnostic> doDiagnostics(DOMDocument xmlDocument, XMLValidationSett

/**
* Do validation with extension (XML Schema, etc)
*
*
* @param xmlDocument
* @param diagnostics
* @param validationSettings
Expand All @@ -55,7 +60,14 @@ private void doExtensionsDiagnostics(DOMDocument xmlDocument, List<Diagnostic> d
XMLValidationSettings validationSettings, CancelChecker monitor) {
for (IDiagnosticsParticipant diagnosticsParticipant : extensionsRegistry.getDiagnosticsParticipants()) {
monitor.checkCanceled();
diagnosticsParticipant.doDiagnostics(xmlDocument, diagnostics, validationSettings, monitor);
try {
diagnosticsParticipant.doDiagnostics(xmlDocument, diagnostics, validationSettings, monitor);
} catch (CancellationException | CacheResourceDownloadingException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing diagnostics for the participant '" + diagnosticsParticipant.getClass().getName() + "'.", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.IDocumentLinkParticipant;
Expand All @@ -28,37 +31,24 @@ class XMLDocumentLink {

private final XMLExtensionsRegistry extensionsRegistry;

private static Logger LOGGER = Logger.getLogger(XMLDocumentLink.class.getName());

public XMLDocumentLink(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}

public List<DocumentLink> findDocumentLinks(DOMDocument document) {
List<DocumentLink> newLinks = new ArrayList<>();
for (IDocumentLinkParticipant participant : extensionsRegistry.getDocumentLinkParticipants()) {
participant.findDocumentLinks(document, newLinks);
try {
participant.findDocumentLinks(document, newLinks);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing document links for the participant '" + participant.getClass().getName() + "'.", e);
}
}
// TODO: call extension
/*
* let rootAbsoluteUrl: Uri | null = null;
*
* Scanner scanner = XMLScanner.createScanner(document.getText(), 0); TokenType
* token = scanner.scan(); let afterHrefOrSrc = false; let afterBase = false;
* let base: string | undefined = void 0; while (token != TokenType.EOS) {
* switch (token) { case TokenType.StartTag: if (!base) { let tagName =
* scanner.getTokenText().toLowerCase(); afterBase = tagName === 'base'; }
* break; case TokenType.AttributeName: let attributeName =
* scanner.getTokenText().toLowerCase(); afterHrefOrSrc = attributeName ===
* 'src' || attributeName === 'href'; break; case TokenType.AttributeValue: if
* (afterHrefOrSrc) { let attributeValue = scanner.getTokenText(); if
* (!afterBase) { // don't highlight the base link itself let link =
* createLink(document, documentContext, attributeValue,
* scanner.getTokenOffset(), scanner.getTokenEnd(), base); if (link) {
* newLinks.push(link); } } if (afterBase && typeof base === 'undefined') { base
* = normalizeRef(attributeValue, document.languageId); if (base &&
* documentContext) { base = documentContext.resolveReference(base,
* document.uri); } } afterBase = false; afterHrefOrSrc = false; } break; }
* token = scanner.scan(); }
*/
return newLinks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -122,7 +123,14 @@ private void fillWithCustomHighlights(DOMNode node, Position position, int offse
List<DocumentHighlight> highlights, CancelChecker cancelChecker) {
// Consume highlighting participant
for (IHighlightingParticipant highlightingParticipant : extensionsRegistry.getHighlightingParticipants()) {
highlightingParticipant.findDocumentHighlights(node, position, offset, highlights, cancelChecker);
try {
highlightingParticipant.findDocumentHighlights(node, position, offset, highlights, cancelChecker);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while processing highlights for the participant '"
+ highlightingParticipant.getClass().getName() + "'.", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.IReferenceParticipant;
Expand All @@ -31,6 +34,8 @@ class XMLReference {

private final XMLExtensionsRegistry extensionsRegistry;

private static Logger LOGGER = Logger.getLogger(XMLReference.class.getName());

public XMLReference(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}
Expand All @@ -39,7 +44,14 @@ public List<? extends Location> findReferences(DOMDocument document, Position po
CancelChecker cancelChecker) {
List<Location> locations = new ArrayList<>();
for (IReferenceParticipant participant : extensionsRegistry.getReferenceParticipants()) {
participant.findReference(document, position, context, locations, cancelChecker);
try {
participant.findReference(document, position, context, locations, cancelChecker);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing references for the participant '" + participant.getClass().getName() + "'.", e);
}
}
return locations;
}
Expand Down
Loading

0 comments on commit 2bfb29a

Please sign in to comment.