Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion apps/frontend/src/api/cliparrClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import assert from "node:assert/strict";
import test from "node:test";
import { cliparrClient, subscribeToAuthFailure } from "@/api/cliparrClient";
import {
cliparrClient,
readResponseErrorDetails,
subscribeToAuthFailure,
} from "@/api/cliparrClient";

function jsonResponse(value: unknown, init: ResponseInit = {}) {
return Response.json(value, {
Expand Down Expand Up @@ -221,3 +225,57 @@ void test("follows app auth redirects with the current browser location", async
}
}
});

void test("preserves actionable subtitle errors and tolerates non-JSON or malformed error bodies", async () => {
const message =
"The selected Plex subtitle changed. Refresh the editor and try again.";
assert.deepEqual(
await readResponseErrorDetails(
jsonResponse(
{
error: {
code: "plex_subtitle_selection_changed",
message,
},
},
{ status: 409 },
),
),
{ code: "plex_subtitle_selection_changed", message },
);

for (const body of ["not JSON", "null", '{"error":42}']) {
assert.deepEqual(
await readResponseErrorDetails(
new Response(body, {
status: 502,
headers: { "content-type": "application/json" },
}),
),
{},
);
}
assert.deepEqual(
await readResponseErrorDetails(
new Response("<html>Bad gateway</html>", {
status: 502,
headers: { "content-type": "text/html" },
}),
),
{},
);
});

void test("propagates cancellation while reading an API error body", async () => {
const response = new Response(
new ReadableStream<Uint8Array>({
start(controller) {
controller.error(new DOMException("Download cancelled", "AbortError"));
},
}),
{ status: 409, headers: { "content-type": "application/json" } },
);
await assert.rejects(readResponseErrorDetails(response), {
name: "AbortError",
});
});
20 changes: 16 additions & 4 deletions apps/frontend/src/api/cliparrClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ function responseErrorDetails(payload: unknown): ResponseErrorDetails {
};
}

export async function readResponseErrorDetails(
response: Response,
): Promise<ResponseErrorDetails> {
if (!response.headers.get("content-type")?.includes("application/json")) {
return {};
}

const text = await response.text();
try {
return responseErrorDetails(JSON.parse(text));
} catch {
return {};
}
}

function queueAuthFailureNotification() {
if (authFailureQueued) {
return;
Expand Down Expand Up @@ -144,10 +159,7 @@ async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
throw buildUnexpectedApiResponseError();
}

const data: unknown = contentType.includes("application/json")
? await response.json().catch((): unknown => null)
: null;
const error = responseErrorDetails(data);
const error = await readResponseErrorDetails(response);

if (response.status === 401 && error.code === "not_authenticated") {
queueAuthFailureNotification();
Expand Down
12 changes: 8 additions & 4 deletions apps/frontend/src/components/editor/useSubtitleCues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
logEventFields,
} from "@cliparr/shared/logging";
import type { PlaybackSubtitleTrack } from "@/providers/types";
import { readResponseErrorDetails } from "@/api/cliparrClient";
import {
subtitleTrackKey,
subtitleTrackSupportsBurnIn,
Expand Down Expand Up @@ -39,10 +40,13 @@ type SubtitleDownloadResult =
failure: SubtitleDownloadFailure;
};

function subtitleDownloadFailure(status: number): SubtitleDownloadFailure {
async function subtitleDownloadFailure(
response: Response,
): Promise<SubtitleDownloadFailure> {
const error = await readResponseErrorDetails(response);
return {
status,
message: `Could not load subtitles (${status}).`,
status: response.status,
message: error.message ?? `Could not load subtitles (${response.status}).`,
};
}

Expand Down Expand Up @@ -70,7 +74,7 @@ async function downloadSubtitleCues(
if (!response.ok) {
return {
ok: false,
failure: subtitleDownloadFailure(response.status),
failure: await subtitleDownloadFailure(response),
};
}

Expand Down
Loading