Merge branch 'handle-translate-error-not-supported' into 'main'

feat: return translation not supported error to the frontend

See merge request soapbox-pub/ditto!539
This commit is contained in:
Alex Gleason 2024-10-09 22:24:09 +00:00
commit 0819049cb8
3 changed files with 10 additions and 1 deletions

View file

@ -132,7 +132,10 @@ const translateController: AppController = async (c) => {
dittoTranslations.set(translatedId, { data: mastodonTranslation }); dittoTranslations.set(translatedId, { data: mastodonTranslation });
return c.json(mastodonTranslation, 200); return c.json(mastodonTranslation, 200);
} catch { } catch (e) {
if (e instanceof Error && e.message?.includes('not supported')) {
return c.json({ error: `Translation of source language '${event.language}' not supported` }, 422);
}
return c.json({ error: 'Service Unavailable' }, 503); return c.json({ error: 'Service Unavailable' }, 503);
} }
}; };

View file

@ -68,6 +68,9 @@ export class DeepLTranslator implements DittoTranslator {
const response = await this.fetch(request); const response = await this.fetch(request);
const json = await response.json(); const json = await response.json();
if (!response.ok) {
throw new Error(json['message']);
}
const data = DeepLTranslator.schema().parse(json); const data = DeepLTranslator.schema().parse(json);
return data; return data;

View file

@ -68,6 +68,9 @@ export class LibreTranslateTranslator implements DittoTranslator {
const response = await this.fetch(request); const response = await this.fetch(request);
const json = await response.json(); const json = await response.json();
if (!response.ok) {
throw new Error(json['error']);
}
const data = LibreTranslateTranslator.schema().parse(json); const data = LibreTranslateTranslator.schema().parse(json);
return data; return data;