Skip to content

Commit

Permalink
Merge pull request #4683 from coralproject/fix/CORL-3195-non-error-ex…
Browse files Browse the repository at this point in the history
…ceptions

[CORL-3195]: more error handling and type check fixes
  • Loading branch information
tessalt authored Oct 22, 2024
2 parents 4208938 + d492591 commit 8094b3e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions server/src/core/server/app/handlers/api/tenor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ export const tenorSearchHandler =
return;
}

const gifsEnabled = tenant.media?.gifs.enabled ?? false;
const gifsEnabled = tenant.media?.gifs?.enabled ?? false;
if (!gifsEnabled) {
res.status(200).send({
results: [],
});
return;
}

const apiKey = tenant.media?.gifs.key ?? null;
const apiKey = tenant.media?.gifs?.key ?? null;
if (!apiKey || apiKey.length === 0) {
res.status(200).send({
results: [],
Expand All @@ -86,7 +86,7 @@ export const tenorSearchHandler =
}

const contentFilter = convertGiphyContentRatingToTenorLevel(
tenant.media?.gifs.maxRating
tenant.media?.gifs?.maxRating
);

const url = new URL(TENOR_SEARCH_URL);
Expand Down Expand Up @@ -133,7 +133,7 @@ export const tenorSearchHandler =
} 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]");
e.message = e.message.replace(tenant.media?.gifs?.key, "[Sensitive]");
}
throw new WrappedInternalError(e as Error, "tenor search error");
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/core/server/models/tenant/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ export function supportsMediaType(
return !!tenant.media?.youtube.enabled;
case "giphy":
return (
!!tenant.media?.gifs.enabled &&
!!tenant.media?.gifs?.enabled &&
!!tenant.media.gifs.key &&
tenant.media.gifs.provider === GQLGIF_MEDIA_SOURCE.GIPHY
);
case "tenor":
return (
!!tenant.media?.gifs.enabled &&
!!tenant.media?.gifs?.enabled &&
!!tenant.media.gifs.key &&
tenant.media.gifs.provider === GQLGIF_MEDIA_SOURCE.TENOR
);
Expand Down
5 changes: 4 additions & 1 deletion server/src/core/server/services/comments/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ async function attachTenorMedia(
video: data.media_formats.mp4.url,
};
} catch (err) {
throw new WrappedInternalError(err as Error, "cannot attach Tenor Media");
if (!(err instanceof Error)) {
throw new Error("cannot attach Tenor Media");
}
throw new WrappedInternalError(err, "cannot attach Tenor Media");
}
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/core/server/services/giphy/giphy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const GiphyRetrieveResponseSchema = Joi.object().keys({

export function ratingIsAllowed(rating: string, tenant: Tenant) {
const compareRating = rating.toLowerCase();
const maxRating = tenant.media?.gifs.maxRating || "g";
const maxRating = tenant.media?.gifs?.maxRating || "g";

const compareIndex = RATINGS_ORDER.indexOf(compareRating);
const maxIndex = RATINGS_ORDER.indexOf(maxRating);
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function searchGiphy(
offset: string,
tenant: Tenant
): Promise<GiphyGifSearchResponse> {
if (!supportsMediaType(tenant, "giphy") || !tenant.media.gifs.key) {
if (!supportsMediaType(tenant, "giphy") || !tenant.media?.gifs?.key) {
throw new InternalError("Giphy was not enabled");
}

Expand Down Expand Up @@ -137,12 +137,12 @@ export async function retrieveFromGiphy(
tenant: Tenant,
id: string
): Promise<GiphyGifRetrieveResponse> {
if (!supportsMediaType(tenant, "giphy") || !tenant.media.gifs.key) {
if (!supportsMediaType(tenant, "giphy") || !tenant.media?.gifs?.key) {
throw new InternalError("Giphy was not enabled");
}

const url = new URL(`${GIPHY_FETCH}/${id}`);
url.searchParams.set("api_key", tenant.media.gifs.key);
url.searchParams.set("api_key", tenant.media?.gifs?.key);

try {
const res = await fetch(url.toString());
Expand All @@ -158,7 +158,7 @@ export async function retrieveFromGiphy(
} catch (err) {
// Ensure that the API key doesn't get leaked to the logs by accident.
if (err.message) {
err.message = err.message.replace(tenant.media.gifs.key, "[Sensitive]");
err.message = err.message.replace(tenant.media?.gifs?.key, "[Sensitive]");
}

// Rethrow the error.
Expand Down
2 changes: 1 addition & 1 deletion server/src/core/server/services/tenor/tenor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function retrieveFromTenor(
tenant: Tenant,
id: string
): Promise<FetchPayload> {
if (!supportsMediaType(tenant, "tenor") || !tenant.media.gifs.key) {
if (!supportsMediaType(tenant, "tenor") || !tenant.media?.gifs?.key) {
throw new InternalError("Tenor was not enabled");
}

Expand Down

0 comments on commit 8094b3e

Please sign in to comment.