Skip to content

Commit 4fc0785

Browse files
fix: Still process if translation fails
1 parent ac1c86c commit 4fc0785

File tree

3 files changed

+33
-54
lines changed

3 files changed

+33
-54
lines changed

OpenAPI.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ paths:
7979
type: string
8080
responses:
8181
'200':
82-
description: Request returned a positive result.
82+
description: >-
83+
Request returned a positive result. If translation fails, results
84+
will be based on untranslated ingredients.
8385
'500':
8486
description: Internal Server Error.
85-
'503':
86-
description: >-
87-
Service Unavailable. Translation service is unavailable. Try again
88-
with disabled translation (Results might vary). Add flag
89-
?translate=false to the request.
9087
tags:
9188
- IngredientsV1
9289
- Ingredients

src/ingredients/tests/ingredientsv1.controller.spec.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,26 @@ describe("IngredientsV1Controller", () => {
185185
});
186186
});
187187

188-
it("should handle translation service unavailable", async () => {
188+
it("should process untranslated ingredients when translation fails", async () => {
189189
const res = mockResponse();
190190
translationService.translateText.mockRejectedValueOnce(
191191
new Error("Translate timed out")
192192
);
193193

194-
await controller.getIngredients("ingredient", res, true);
194+
await controller.getIngredients("tofu,milk,sugar", res, true);
195195

196-
expect(res.status).toHaveBeenCalledWith(HttpStatus.SERVICE_UNAVAILABLE);
196+
expect(res.status).toHaveBeenCalledWith(HttpStatus.OK);
197197
expect(res.send).toHaveBeenCalledWith({
198-
code: "Service Unavailable",
199-
status: "503",
200-
message:
201-
"Translation service is unavailable. Try again with disabled translation (Results might vary). Add flag ?translate=false to the request.",
198+
code: "OK",
199+
status: "200",
200+
message: "Success",
201+
data: {
202+
vegan: false,
203+
surely_vegan: ["tofu"],
204+
not_vegan: ["milk"],
205+
maybe_not_vegan: ["sugar"],
206+
unknown: [],
207+
},
202208
});
203209
});
204210

src/ingredients/v1/ingredients.controller.ts

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,13 @@ export class IngredientsV1Controller implements OnModuleInit {
7777
@ApiTags("Ingredients")
7878
@ApiResponse({
7979
status: 200,
80-
description: "Request returned a positive result.",
80+
description:
81+
"Request returned a positive result. If translation fails, results will be based on untranslated ingredients.",
8182
})
8283
@ApiResponse({
8384
status: 500,
8485
description: "Internal Server Error.",
8586
})
86-
@ApiResponse({
87-
status: 503,
88-
description:
89-
"Service Unavailable. Translation service is unavailable. Try again with disabled translation (Results might vary). Add flag ?translate=false to the request.",
90-
})
9187
async getIngredients(
9288
@Param("ingredients") ingredientsParam: string,
9389
@Res() res: Response,
@@ -128,34 +124,11 @@ export class IngredientsV1Controller implements OnModuleInit {
128124

129125
response = this.parseIngredients(translated);
130126
} catch (error) {
131-
if (error instanceof Error) {
132-
if (error.message === "Translate timed out") {
133-
this.logger.error(`Translation service is unavailable: ${error}`);
134-
res.status(HttpStatus.SERVICE_UNAVAILABLE).send({
135-
code: "Service Unavailable",
136-
status: "503",
137-
message:
138-
"Translation service is unavailable. Try again with disabled translation (Results might vary). Add flag ?translate=false to the request.",
139-
});
140-
return;
141-
} else {
142-
this.logger.error(`Error during translation: ${error}`);
143-
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({
144-
code: "Internal Server Error",
145-
status: "500",
146-
message: "An error occurred during the translation process",
147-
});
148-
return;
149-
}
150-
} else {
151-
this.logger.error(`Unknown error: ${error}`);
152-
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({
153-
code: "Internal Server Error",
154-
status: "500",
155-
message: "An unknown error occurred while processing the request",
156-
});
157-
return;
158-
}
127+
this.logger.warn(
128+
`Translation failed, proceeding with untranslated ingredients: ${error}`
129+
);
130+
response = ingredients;
131+
targetLanguage = "EN";
159132
}
160133
} else {
161134
response = ingredients;
@@ -220,13 +193,16 @@ export class IngredientsV1Controller implements OnModuleInit {
220193
translatedUnknown
221194
);
222195
} catch (error) {
223-
this.logger.error(`Error during back translation: ${error}`);
224-
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({
225-
code: "Internal Server Error",
226-
status: "500",
227-
message: "An error occurred during the back translation process",
228-
});
229-
return;
196+
this.logger.warn(
197+
`Back translation failed, using untranslated results: ${error}`
198+
);
199+
this.sendResponse(
200+
res,
201+
notVeganResult,
202+
maybeNotVeganResult,
203+
veganResult,
204+
unknownResult
205+
);
230206
}
231207
} else {
232208
this.sendResponse(

0 commit comments

Comments
 (0)