backend: properly catch exception in controllers so that the server does not crash

This commit is contained in:
wea_ondara
2023-11-25 17:08:13 +01:00
parent 9c0b266cbb
commit c5cd379b8a
2 changed files with 93 additions and 81 deletions

View File

@@ -13,9 +13,9 @@ export default class AniListController {
const fromApiText = await fromApi.text(); const fromApiText = await fromApi.text();
res.status(fromApi.status).send(fromApiText); res.status(fromApi.status).send(fromApiText);
} catch (e) {
console.error(e);
}
next(); next();
} catch (e) {
next(e);
}
} }
} }

View File

@@ -9,6 +9,7 @@ export class MangaUpdatesController {
} }
async search(req: Request, res: Response, next: NextFunction): Promise<void> { async search(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const bjson = req.body; const bjson = req.body;
if (bjson['stype'] !== 'title' || bjson['type'] !== 'Manga' || !bjson['search']?.trim().length) { if (bjson['stype'] !== 'title' || bjson['type'] !== 'Manga' || !bjson['search']?.trim().length) {
res.status(400).send('Only {stype: "title", type: "Manga", search: "some title"} allowed!'); res.status(400).send('Only {stype: "title", type: "Manga", search: "some title"} allowed!');
@@ -46,9 +47,13 @@ export class MangaUpdatesController {
res.status(200).setHeader('Content-Type', 'application/json').send(fromApiJson); res.status(200).setHeader('Content-Type', 'application/json').send(fromApiJson);
next('router'); next('router');
} catch (e) {
next(e);
}
} }
async getById(req: Request, res: Response, next: NextFunction): Promise<void> { async getById(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const id = req.params.id!.toLowerCase(); const id = req.params.id!.toLowerCase();
const fromCache = this.cache.getSeriesById(id); const fromCache = this.cache.getSeriesById(id);
@@ -74,9 +79,13 @@ export class MangaUpdatesController {
res.status(200).setHeader('Content-Type', 'application/json').send(fromApiJson); res.status(200).setHeader('Content-Type', 'application/json').send(fromApiJson);
next(); next();
} catch (e) {
next(e);
}
} }
async getGroupById(req: Request, res: Response, next: NextFunction): Promise<void> { async getGroupById(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const id = req.params.id!.toLowerCase(); const id = req.params.id!.toLowerCase();
const fromCache = this.cache.getSeriesGroupsById(id); const fromCache = this.cache.getSeriesGroupsById(id);
@@ -102,6 +111,9 @@ export class MangaUpdatesController {
res.status(200).setHeader('Content-Type', 'application/json').send(fromApiJson); res.status(200).setHeader('Content-Type', 'application/json').send(fromApiJson);
next(); next();
} catch (e) {
next(e);
}
} }
async getSeriesIdFromWebsiteId(req: Request, res: Response, next: NextFunction): Promise<void> { async getSeriesIdFromWebsiteId(req: Request, res: Response, next: NextFunction): Promise<void> {