Skip to content

Commit 5d2166e

Browse files
committed
fix(instagram): explain unavailable posts
1 parent d3dba94 commit 5d2166e

3 files changed

Lines changed: 60 additions & 16 deletions

File tree

apps/api/src/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,22 @@ const app = new Hono<{ Bindings: CloudflareBindings }>()
124124
EMBED_USER_AGENT: c.env.EMBED_USER_AGENT,
125125
});
126126
} catch (cause) {
127-
const problem = createProblem(EmbedlyErrors.PlatformFetchFailed, {
127+
const errorContext = getErrorContext(cause);
128+
let event = EmbedlyErrors.PlatformFetchFailed;
129+
if (platform === "Instagram") {
130+
if (errorContext.upstream_reason === "instagram.age_restricted") {
131+
event = EmbedlyErrors.InstagramAgeRestricted;
132+
} else if (errorContext.upstream_reason === "instagram.post_unavailable") {
133+
event = EmbedlyErrors.InstagramPostUnavailable;
134+
} else if (errorContext.upstream_reason === "instagram.media_unavailable") {
135+
event = EmbedlyErrors.InstagramMediaUnavailable;
136+
}
137+
}
138+
const problem = createProblem(event, {
128139
request_id: requestId,
129-
context: { ...logContext, ...getErrorContext(cause) },
140+
context: { ...logContext, ...errorContext },
130141
});
131-
Object.assign(logContext, getErrorContext(cause), {
142+
Object.assign(logContext, errorContext, {
132143
outcome: "error",
133144
status_code: problem.status,
134145
error_type: problem.type,

packages/logging/src/main.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ErrorContext {
88
error_message?: string;
99
upstream_status?: LogValue;
1010
upstream_message?: LogValue;
11+
upstream_reason?: LogValue;
1112
}
1213

1314
export interface EmbedlyEvent {
@@ -62,6 +63,25 @@ export const EmbedlyErrors = {
6263
detail: "Embedly could not fetch that post from the platform.",
6364
status: 502,
6465
}),
66+
InstagramAgeRestricted: defineError({
67+
type: "instagram.age_restricted",
68+
title: "Age-restricted content.",
69+
detail: "Instagram requires login to view this post. Embedly can only access public posts.",
70+
status: 403,
71+
}),
72+
InstagramPostUnavailable: defineError({
73+
type: "instagram.post_unavailable",
74+
title: "Post unavailable.",
75+
detail: "Instagram isn't making this post available without login.",
76+
status: 404,
77+
}),
78+
InstagramMediaUnavailable: defineError({
79+
type: "platform.fetch_failed",
80+
title: "Couldn't load this post.",
81+
detail:
82+
"Instagram didn't provide public media for this post. It may require login or be unavailable.",
83+
status: 502,
84+
}),
6585
PlatformTransformFailed: defineError({
6686
type: "platform.transform_failed",
6787
title: "Failed to build post data.",
@@ -250,6 +270,7 @@ export function getErrorContext(error: unknown): ErrorContext {
250270
if ("code" in error) context.upstream_status = toLogValue(error.code);
251271
if ("status" in error) context.upstream_status = toLogValue(error.status);
252272
if ("message" in error) context.upstream_message = toLogValue(error.message);
273+
if ("reason" in error) context.upstream_reason = toLogValue(error.reason);
253274
return context;
254275
}
255276

packages/platforms/src/platforms/instagram.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,36 @@ export const Instagram: Platform<"Instagram", InstagramMedia, {}> = {
101101
(text) => text.includes("RelayPrefetchedStreamCache") && text.includes(PRELOADER_PREFIX),
102102
);
103103

104-
if (!script) {
105-
throw {
106-
code: 500,
107-
message: "Instagram page structure changed: missing data script",
108-
};
109-
}
110-
111104
let media: InstagramMedia | undefined | null;
112-
try {
113-
media = parseRelayMedia(script);
114-
} catch {
115-
throw { code: 500, message: "Failed to parse Instagram data" };
105+
if (script) {
106+
try {
107+
media = parseRelayMedia(script);
108+
} catch {
109+
throw { code: 500, message: "Failed to parse Instagram data" };
110+
}
116111
}
117112

118113
if (!media) {
114+
$("script, style").remove();
115+
const text = $("body")
116+
.text()
117+
.replace(/\s+/g, " ")
118+
.replace(/\u2019/g, "'");
119+
let reason = "instagram.media_unavailable";
120+
let code = 500;
121+
if (text.includes("Age-restricted content")) {
122+
reason = "instagram.age_restricted";
123+
code = 403;
124+
} else if (text.includes("Post isn't available")) {
125+
reason = "instagram.post_unavailable";
126+
code = 404;
127+
}
119128
throw {
120-
code: 500,
121-
message: "Instagram page structure changed: missing media data",
129+
code,
130+
reason,
131+
message: script
132+
? "Instagram page structure changed: missing media data"
133+
: "Instagram page structure changed: missing data script",
122134
};
123135
}
124136

0 commit comments

Comments
 (0)