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 #946

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 15, 2020
1 parent e2f31b8 commit f55935e
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 63 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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -366,11 +367,18 @@ private boolean processSymbolsParticipants(DOMDocument xmlDocument, SymbolInform
LOGGER.log(Level.WARNING, "There are '" + replaceParticipants.size() + "' replace participants");
}
for (ISymbolsProviderParticipant replaceParticipant : replaceParticipants) {
if (symbolInformations != null) {
replaceParticipant.findSymbolInformations(xmlDocument, symbolInformations, filter, cancelChecker);
}
if (documentSymbols != null) {
replaceParticipant.findDocumentSymbols(xmlDocument, documentSymbols, filter, cancelChecker);
try {
if (symbolInformations != null) {
replaceParticipant.findSymbolInformations(xmlDocument, symbolInformations, filter, cancelChecker);
}
if (documentSymbols != null) {
replaceParticipant.findDocumentSymbols(xmlDocument, documentSymbols, filter, cancelChecker);
}
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing symbols for '" + replaceParticipant.getClass().getName() + "': " + e.getMessage());
}
}
return true;
Expand All @@ -380,11 +388,20 @@ private boolean processSymbolsParticipants(DOMDocument xmlDocument, SymbolInform
Collection<ISymbolsProviderParticipant> insertParticipants = resultParticipant.getInsertParticipants();
if (!insertParticipants.isEmpty()) {
for (ISymbolsProviderParticipant insertParticipant : insertParticipants) {
if (symbolInformations != null) {
insertParticipant.findSymbolInformations(xmlDocument, symbolInformations, filter, cancelChecker);
}
if (documentSymbols != null) {
insertParticipant.findDocumentSymbols(xmlDocument, documentSymbols, filter, cancelChecker);
try {
if (symbolInformations != null) {
insertParticipant.findSymbolInformations(xmlDocument, symbolInformations, filter, cancelChecker);
}
if (documentSymbols != null) {
insertParticipant.findDocumentSymbols(xmlDocument, documentSymbols, filter, cancelChecker);
}
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing symbols for the participant '"
+ insertParticipant.getClass().getName() + "'.",
e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 @@ -51,7 +52,14 @@ public List<? extends LocationLink> findTypeDefinition(DOMDocument document, Pos
}
List<LocationLink> locations = new ArrayList<>();
for (ITypeDefinitionParticipant participant : extensionsRegistry.getTypeDefinitionParticipants()) {
participant.findTypeDefinition(request, locations, cancelChecker);
try {
participant.findTypeDefinition(request, locations, cancelChecker);
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Error while processing type definitions for the participant '" + participant.getClass().getName() + "'.", e);
}
}
return locations;
}
Expand Down
Loading

0 comments on commit f55935e

Please sign in to comment.