Skip to content

Commit 35281e5

Browse files
committed
fix: Add catch all not a feed error
1 parent 60beae9 commit 35281e5

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

src/handlers/feed.ts

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { BaseError, EmptyParserOutputError } from "../errors";
1+
import {
2+
BaseError,
3+
EmptyParserOutputError,
4+
NotAFeedError,
5+
ParserError,
6+
} from "../errors";
27
import { parserKeys, parsers } from "../parsers";
38
import request from "../request";
49
import transform from "../transform";
@@ -18,7 +23,7 @@ export async function parseFromString({
1823
parser?: ParserKey;
1924
}): Promise<ParserResponse> {
2025
if (parser) {
21-
return parse(parser, content);
26+
return await parse(parser, content);
2227
}
2328
for (let i = 0; i < parserKeys.length; i++) {
2429
try {
@@ -55,31 +60,41 @@ export async function parseFromQuery({
5560
const isJsonFeed = ["application/json", "application/feed+json"].includes(
5661
contentType,
5762
);
58-
const parsed = await parseFromString({
59-
content: response.text,
60-
parser: isJsonFeed ? "JSON_FEED_V1" : parser,
61-
});
63+
const isXmlFeed =
64+
["application/xml", "text/xml"].includes(contentType) &&
65+
/application\/\w+\+xml/;
66+
try {
67+
const parsed = await parseFromString({
68+
content: response.text,
69+
parser: isJsonFeed ? "JSON_FEED_V1" : parser,
70+
});
6271

63-
parsed.items = parsed.items?.filter((item) => {
64-
if (item == null) {
65-
return false;
66-
}
67-
if (
68-
item.date_published &&
69-
endTime &&
70-
new Date(endTime) < new Date(item.date_published)
71-
) {
72-
return false;
73-
}
74-
if (
75-
item.date_published &&
76-
startTime &&
77-
new Date(startTime) > new Date(item.date_published)
78-
) {
79-
return false;
72+
parsed.items = parsed.items?.filter((item) => {
73+
if (item == null) {
74+
return false;
75+
}
76+
if (
77+
item.date_published &&
78+
endTime &&
79+
new Date(endTime) < new Date(item.date_published)
80+
) {
81+
return false;
82+
}
83+
if (
84+
item.date_published &&
85+
startTime &&
86+
new Date(startTime) > new Date(item.date_published)
87+
) {
88+
return false;
89+
}
90+
return true;
91+
});
92+
parsed.feed_url = parsed.feed_url || url;
93+
return parsed;
94+
} catch (error: any) {
95+
if (error instanceof ParserError && !isJsonFeed && !isXmlFeed) {
96+
throw new NotAFeedError();
8097
}
81-
return true;
82-
});
83-
parsed.feed_url = parsed.feed_url || url;
84-
return parsed;
98+
throw error;
99+
}
85100
}

0 commit comments

Comments
 (0)