Skip to content

Commit

Permalink
fix: don't log same BadLocationException excessively (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom authored Aug 28, 2024
1 parent a922a85 commit e44f107
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,34 @@
*******************************************************************************/
package org.eclipse.lsp4e;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.lsp4e.ui.LSPImages;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.google.common.base.Throwables;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;

public class LanguageServerPlugin extends AbstractUIPlugin {

/**
* Used by {@link #logError(String, Throwable)} to prevent logging the same
* exception of certain types repeatedly
*/
private static final ConcurrentMap<HashCode, Integer> EXCEPTIONS_COUNTER = new ConcurrentHashMap<>();

/** Job family identifier for the "background update markers from diagnostics" job. */
public static final Object FAMILY_UPDATE_MARKERS = new Object();
/** Job family identifier for the "initialize language server" job. */
Expand Down Expand Up @@ -73,9 +89,7 @@ protected void initializeImageRegistry(ImageRegistry registry) {
* The exception through which we noticed the error
*/
public static void logError(final Throwable thr) {
if (plugin != null) {
plugin.getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, thr.getMessage(), thr));
}
logError(thr.getMessage(), thr);
}

/**
Expand All @@ -87,7 +101,14 @@ public static void logError(final Throwable thr) {
* The exception through which we noticed the error
*/
public static void logError(final @Nullable String message, final @Nullable Throwable thr) {
final var plugin = LanguageServerPlugin.plugin;
if (plugin != null) {
if (!DEBUG && thr instanceof BadLocationException) {
final HashCode key = Hashing.sha256().hashString(Throwables.getStackTraceAsString(thr), UTF_8);
if (EXCEPTIONS_COUNTER.getOrDefault(key, 0) > 2)
return;
EXCEPTIONS_COUNTER.compute(key, (k, v) -> v == null ? 1 : ++v);
}
plugin.getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, message, thr));
}
}
Expand Down

0 comments on commit e44f107

Please sign in to comment.