Skip to content

Commit 0824952

Browse files
committed
feat: Add custom error for cloudflare block
1 parent 10307e6 commit 0824952

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ export class InvalidUrlError extends BaseError {
147147
}
148148
}
149149

150+
export class CloudflareBlockError extends BaseError {
151+
constructor() {
152+
super("Blocked by cloudflare", "cloudflare-block");
153+
}
154+
}
155+
150156
export function createErrorFormatter(
151157
Sentry: any,
152158
): ApolloServerOptions<any>["formatError"] {

src/request.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import _debug from "debug";
44
const debug = _debug("graphql-rss-parser:request");
55

66
import {
7+
CloudflareBlockError,
78
ConnectionRefusedError,
89
DnsLookupError,
910
EmptyHttpResponseError,
@@ -43,6 +44,15 @@ export default async function request(url: string) {
4344
} catch (error: any) {
4445
debug(`request to ${url} failed with error`, error);
4546
if (error.response?.status) {
47+
if (error.response?.status === 403) {
48+
const text = error.response.data.toString();
49+
if (
50+
text.includes("Enable JavaScript and cookies to continue") &&
51+
text.includes("_cf_")
52+
) {
53+
throw new CloudflareBlockError();
54+
}
55+
}
4656
throw new UpstreamHttpError("Upstream HTTP error", error.response.status);
4757
}
4858
if (error.code === "ENOTFOUND" || error.code === "EAI_AGAIN") {

0 commit comments

Comments
 (0)