Skip to content

Commit

Permalink
Merge pull request #4679 from coralproject/fix/CORL-3195-tenor-error-…
Browse files Browse the repository at this point in the history
…handling

[CORL-3195]: update tenor search
  • Loading branch information
tessalt authored Oct 7, 2024
2 parents 32ed22d + 044f417 commit 07549e4
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions server/src/core/server/app/handlers/api/tenor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from "node-fetch";

import { SearchPayload } from "coral-common/common/lib/types/tenor";
import { AppOptions } from "coral-server/app/";
import { WrappedInternalError } from "coral-server/errors";
import { RequestHandler, TenantCoralRequest } from "coral-server/types/express";

const SEARCH_LIMIT = 32;
Expand Down Expand Up @@ -58,7 +59,7 @@ export const tenorSearchHandler =

const result = schema.validate(req.query);
if (result.error || result.errors) {
res.status(400).send(result.errors);
res.status(400).send(result.error ?? result.errors);
return;
}

Expand Down Expand Up @@ -99,33 +100,41 @@ export const tenorSearchHandler =
url.searchParams.set("pos", params.pos);
}

const response = await fetch(url.toString(), {
method: "GET",
});

if (!response.ok) {
res.status(500).send({
results: [],
try {
const response = await fetch(url.toString(), {
method: "GET",
});
return;
}

const json = (await response.json()) as SearchPayload;
if (!json) {
res.status(500).send({
results: [],
if (!response.ok) {
res.status(500).send({
results: [],
});
return;
}
const json = (await response.json()) as SearchPayload;
if (!json) {
res.status(500).send({
results: [],
});
return;
}

res.status(200).send({
results: json.results.map((r) => {
return {
id: r.id,
title: r.title,
url: r.media_formats.gif.url,
preview: r.media_formats.nanogif.url,
};
}),
next: json.next,
});
} catch (e) {
// Ensure that the API key doesn't get leaked to the logs by accident.
if (e.message) {
e.message = e.message.replace(tenant.media?.gifs.key, "[Sensitive]");
}
throw new WrappedInternalError(e as Error, "tenor search error");
}

res.status(200).send({
results: json.results.map((r) => {
return {
id: r.id,
title: r.title,
url: r.media_formats.gif.url,
preview: r.media_formats.nanogif.url,
};
}),
next: json.next,
});
};

0 comments on commit 07549e4

Please sign in to comment.