Skip to content

Commit

Permalink
Ignoring NoHttpResponseException
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Dec 2, 2023
1 parent 1b7d51d commit f91d6ce
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Spring Boot-based Java application to forward Reddit posts to Telegram channel.
cd target
```
* Or
download [executable JAR](https://github.com/yvasyliev/reddit-telegram-forwarder/releases/latest/download/reddit-telegram-forwarder-3.0.0-beta3.jar)
download [executable JAR](https://github.com/yvasyliev/reddit-telegram-forwarder/releases/latest/download/reddit-telegram-forwarder-3.0.0.jar)
from the [latest release](https://github.com/yvasyliev/reddit-telegram-forwarder/releases/latest).
4. Run the app:
```shell
java -jar reddit-telegram-forwarder-2.3.52.jar \
java -jar reddit-telegram-forwarder-3.0.0.jar \
--reddit.client.id=${REDDIT_CLIENT_ID} \
--reddit.client.secret=${REDDIT_CLIENT_SECRET} \
--reddit.password=${REDDIT_PASSWORD} \
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.github.yvasyliev</groupId>
<artifactId>reddit-telegram-forwarder</artifactId>
<version>3.0.0-beta3</version>
<version>3.0.0</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/github/yvasyliev/appenders/IgnoredException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.yvasyliev.appenders;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.config.plugins.PluginValue;

@Plugin(name = "IgnoredException", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class IgnoredException {
private final Class<? extends Throwable> exceptionClass;

public IgnoredException(Class<? extends Throwable> exceptionClass) {
this.exceptionClass = exceptionClass;
}

@PluginFactory
public static IgnoredException createAppender(@PluginValue("value") String name) throws ClassNotFoundException {
return new IgnoredException(Class.forName(name).asSubclass(Throwable.class));
}

public Class<? extends Throwable> getExceptionClass() {
return exceptionClass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.yvasyliev.appenders;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

@Plugin(name = "IgnoredExceptions", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class IgnoredExceptions {
private final Set<Class<? extends Throwable>> exceptionClasses;

public IgnoredExceptions(Set<Class<? extends Throwable>> exceptionClasses) {
this.exceptionClasses = exceptionClasses;
}

@PluginFactory
public static IgnoredExceptions createAppender(@PluginElement("IgnoredException") IgnoredException[] ignoredExceptions) {
return new IgnoredExceptions(
Arrays
.stream(ignoredExceptions)
.map(IgnoredException::getExceptionClass)
.collect(Collectors.toSet())
);
}

public Set<Class<? extends Throwable>> getExceptionClasses() {
return exceptionClasses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.github.yvasyliev.bots.telegram.notifier.TelegramNotifier;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
Expand All @@ -14,46 +12,60 @@
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Optional;
import java.util.Set;

@Plugin(name = "TelegramBotAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class TelegramBotAppender extends AbstractAppender {
private static final Logger LOGGER = LoggerFactory.getLogger(TelegramBotAppender.class);

private static final String MESSAGE_TEMPLATE = """
%s
%s""";

protected TelegramBotAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties) {
super(name, filter, layout, ignoreExceptions, properties);
private final Set<Class<? extends Throwable>> ignoredExceptions;

protected TelegramBotAppender(String name, Set<Class<? extends Throwable>> ignoredExceptions) {
super(name, null, PatternLayout.createDefaultLayout(), true, Property.EMPTY_ARRAY);
this.ignoredExceptions = ignoredExceptions;
}

@PluginFactory
public static TelegramBotAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") Filter filter) {
return new TelegramBotAppender(name, filter, PatternLayout.createDefaultLayout(), true, Property.EMPTY_ARRAY);
public static TelegramBotAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("IgnoredExceptions") IgnoredExceptions ignoredExceptions) {
return new TelegramBotAppender(name, ignoredExceptions.getExceptionClasses());
}

@Override
public void append(LogEvent event) {
RedditTelegramForwarderApplication.withContext(applicationContext -> {
try {
applicationContext
.getBean(TelegramNotifier.class)
.applyWithException(buildMessage(event));
} catch (Exception e) {
e.printStackTrace(System.err);
}
});
var thrown = event.getThrown();
if (thrown == null || !ignoredExceptions.contains(thrown.getClass())) {
RedditTelegramForwarderApplication.withContext(applicationContext -> {
try {
applicationContext
.getBean(TelegramNotifier.class)
.applyWithException(buildMessage(
event.getMessage().getFormattedMessage(),
thrown
));
} catch (Exception e) {
LOGGER.warn("Failed to send notification", e);
}
});
}
}

private String buildMessage(LogEvent event) {
var formattedMessage = event.getMessage().getFormattedMessage();
return getStackTrace(event.getThrown())
.map(stackTrace -> MESSAGE_TEMPLATE.formatted(formattedMessage, stackTrace))
.orElse(formattedMessage);
private String buildMessage(String message, Throwable throwable) {
return getStackTrace(throwable)
.map(stackTrace -> MESSAGE_TEMPLATE.formatted(message, stackTrace))
.orElse(message);
}

private Optional<String> getStackTrace(Throwable throwable) {
Expand All @@ -62,7 +74,7 @@ private Optional<String> getStackTrace(Throwable throwable) {
t.printStackTrace(printWriter);
return stringWriter.toString();
} catch (IOException e) {
e.printStackTrace(System.err);
LOGGER.warn("Failed to build stack trace.", e);
return null;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void publishPosts(List<Post> posts) {
posts.forEach(this::publishPost);
}

public <T extends Post> void publishPost(T post) {
public void publishPost(Post post) {
if (context.containsBean(post.getType())) {
try {
publishPost(post, post.getType());
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<TelegramBotAppender name="telegramBotAppender"/>
<TelegramBotAppender name="telegramBotAppender">
<IgnoredExceptions>
<IgnoredException>org.apache.http.NoHttpResponseException</IgnoredException>
</IgnoredExceptions>
</TelegramBotAppender>
</Appenders>
<Loggers>
<Root level="ALL">
Expand Down

0 comments on commit f91d6ce

Please sign in to comment.