don't expose rome's FeedException

This commit is contained in:
Athou
2025-02-18 08:57:24 +01:00
parent 6729ebc6ea
commit 60fdc79563
2 changed files with 21 additions and 8 deletions

View File

@@ -17,9 +17,9 @@ import com.commafeed.backend.HttpGetter.NotModifiedException;
import com.commafeed.backend.HttpGetter.SchemeNotAllowedException; import com.commafeed.backend.HttpGetter.SchemeNotAllowedException;
import com.commafeed.backend.HttpGetter.TooManyRequestsException; import com.commafeed.backend.HttpGetter.TooManyRequestsException;
import com.commafeed.backend.feed.parser.FeedParser; import com.commafeed.backend.feed.parser.FeedParser;
import com.commafeed.backend.feed.parser.FeedParser.FeedParsingException;
import com.commafeed.backend.feed.parser.FeedParserResult; import com.commafeed.backend.feed.parser.FeedParserResult;
import com.commafeed.backend.urlprovider.FeedURLProvider; import com.commafeed.backend.urlprovider.FeedURLProvider;
import com.rometools.rome.io.FeedException;
import io.quarkus.arc.All; import io.quarkus.arc.All;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
@@ -43,7 +43,7 @@ public class FeedFetcher {
} }
public FeedFetcherResult fetch(String feedUrl, boolean extractFeedUrlFromHtml, String lastModified, String eTag, public FeedFetcherResult fetch(String feedUrl, boolean extractFeedUrlFromHtml, String lastModified, String eTag,
Instant lastPublishedDate, String lastContentHash) throws FeedException, IOException, NotModifiedException, Instant lastPublishedDate, String lastContentHash) throws FeedParsingException, IOException, NotModifiedException,
TooManyRequestsException, SchemeNotAllowedException, HostNotAllowedException, NoFeedFoundException { TooManyRequestsException, SchemeNotAllowedException, HostNotAllowedException, NoFeedFoundException {
log.debug("Fetching feed {}", feedUrl); log.debug("Fetching feed {}", feedUrl);
@@ -53,7 +53,7 @@ public class FeedFetcher {
FeedParserResult parserResult; FeedParserResult parserResult;
try { try {
parserResult = parser.parse(result.getUrlAfterRedirect(), content); parserResult = parser.parse(result.getUrlAfterRedirect(), content);
} catch (FeedException e) { } catch (FeedParsingException e) {
if (extractFeedUrlFromHtml) { if (extractFeedUrlFromHtml) {
String extractedUrl = extractFeedUrl(urlProviders, feedUrl, new String(result.getContent(), StandardCharsets.UTF_8)); String extractedUrl = extractFeedUrl(urlProviders, feedUrl, new String(result.getContent(), StandardCharsets.UTF_8));
if (StringUtils.isNotBlank(extractedUrl)) { if (StringUtils.isNotBlank(extractedUrl)) {
@@ -115,7 +115,7 @@ public class FeedFetcher {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public NoFeedFoundException(Throwable cause) { public NoFeedFoundException(Throwable cause) {
super("No feed found at this URL", cause); super("This URL does not point to an RSS feed or a website with an RSS feed.", cause);
} }
} }

View File

@@ -35,7 +35,6 @@ import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.feed.synd.SyndLink; import com.rometools.rome.feed.synd.SyndLink;
import com.rometools.rome.feed.synd.SyndLinkImpl; import com.rometools.rome.feed.synd.SyndLinkImpl;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedInput;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
@@ -56,12 +55,12 @@ public class FeedParser {
private final EncodingDetector encodingDetector; private final EncodingDetector encodingDetector;
private final FeedCleaner feedCleaner; private final FeedCleaner feedCleaner;
public FeedParserResult parse(String feedUrl, byte[] xml) throws FeedException { public FeedParserResult parse(String feedUrl, byte[] xml) throws FeedParsingException {
try { try {
Charset encoding = encodingDetector.getEncoding(xml); Charset encoding = encodingDetector.getEncoding(xml);
String xmlString = feedCleaner.trimInvalidXmlCharacters(new String(xml, encoding)); String xmlString = feedCleaner.trimInvalidXmlCharacters(new String(xml, encoding));
if (xmlString == null) { if (xmlString == null) {
throw new FeedException("Input string is null for url " + feedUrl); throw new FeedParsingException("Input string is null for url " + feedUrl);
} }
xmlString = feedCleaner.replaceHtmlEntitiesWithNumericEntities(xmlString); xmlString = feedCleaner.replaceHtmlEntitiesWithNumericEntities(xmlString);
xmlString = feedCleaner.removeDoctypeDeclarations(xmlString); xmlString = feedCleaner.removeDoctypeDeclarations(xmlString);
@@ -81,8 +80,10 @@ public class FeedParser {
Long averageEntryInterval = averageTimeBetweenEntries(entries); Long averageEntryInterval = averageTimeBetweenEntries(entries);
return new FeedParserResult(title, link, lastPublishedDate, averageEntryInterval, lastEntryDate, entries); return new FeedParserResult(title, link, lastPublishedDate, averageEntryInterval, lastEntryDate, entries);
} catch (FeedParsingException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new FeedException(String.format("Could not parse feed from %s : %s", feedUrl, e.getMessage()), e); throw new FeedParsingException(String.format("Could not parse feed from %s : %s", feedUrl, e.getMessage()), e);
} }
} }
@@ -268,4 +269,16 @@ public class FeedParser {
return (long) stats.getMean(); return (long) stats.getMean();
} }
public static class FeedParsingException extends Exception {
private static final long serialVersionUID = 1L;
public FeedParsingException(String message) {
super(message);
}
public FeedParsingException(String message, Throwable cause) {
super(message, cause);
}
}
} }