From ad3cdf4ee8a855d3f897b440873be453926517f3 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 9 Sep 2024 14:42:17 +0200 Subject: [PATCH] feat: return 502 for PDF generator errors (#309) (cherry picked from commit 3a8f9376e464de0ddb16472fcd812579d776abc9) --- src/controller/debtor-controller.ts | 5 +++++ src/controller/invoice-controller.ts | 6 +++++- src/controller/payout-request-controller.ts | 5 +++++ src/controller/seller-payout-controller.ts | 5 +++++ src/controller/user-controller.ts | 9 +++++++++ test/unit/controller/debtor-controller.ts | 4 ++-- test/unit/controller/user-controller.ts | 8 ++++---- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/controller/debtor-controller.ts b/src/controller/debtor-controller.ts index b987e4058..7ae471e6f 100644 --- a/src/controller/debtor-controller.ts +++ b/src/controller/debtor-controller.ts @@ -29,6 +29,7 @@ import { In } from 'typeorm'; import { HandoutFinesRequest } from './request/debtor-request'; import Fine from '../entity/fine/fine'; import { ReturnFileType } from 'pdf-generator-client'; +import { PdfError } from '../errors'; export default class DebtorController extends BaseController { private logger: Logger = log4js.getLogger(' DebtorController'); @@ -374,6 +375,10 @@ export default class DebtorController extends BaseController { res.send(buffer); } catch (error) { this.logger.error('Could not get fine report pdf:', error); + if (error instanceof PdfError) { + res.status(502).json('PDF Generator service failed.'); + return; + } res.status(500).json('Internal server error.'); } } diff --git a/src/controller/invoice-controller.ts b/src/controller/invoice-controller.ts index 69b69a8b5..b0ce7b694 100644 --- a/src/controller/invoice-controller.ts +++ b/src/controller/invoice-controller.ts @@ -39,7 +39,7 @@ import { UpdateInvoiceUserRequest } from './request/user-request'; import InvoiceUser from '../entity/user/invoice-user'; import { parseInvoiceUserToResponse } from '../helpers/revision-to-response'; import { AppDataSource } from '../database/database'; -import { NotImplementedError } from '../errors'; +import { NotImplementedError, PdfError } from '../errors'; export default class InvoiceController extends BaseController { private logger: Logger = log4js.getLogger('InvoiceController'); @@ -354,6 +354,10 @@ export default class InvoiceController extends BaseController { res.status(200).json({ pdf: pdf.downloadName }); } catch (error) { this.logger.error('Could get invoice PDF:', error); + if (error instanceof PdfError) { + res.status(502).json('PDF Generator service failed.'); + return; + } res.status(500).json('Internal server error.'); } } diff --git a/src/controller/payout-request-controller.ts b/src/controller/payout-request-controller.ts index 4ddbdfa65..bd6c9c7f3 100644 --- a/src/controller/payout-request-controller.ts +++ b/src/controller/payout-request-controller.ts @@ -30,6 +30,7 @@ import PayoutRequestRequest from './request/payout-request-request'; import User from '../entity/user/user'; import BalanceService from '../service/balance-service'; import { PdfUrlResponse } from './response/simple-file-response'; +import { PdfError } from '../errors'; export default class PayoutRequestController extends BaseController { private logger: Logger = log4js.getLogger('PayoutRequestController'); @@ -294,6 +295,10 @@ export default class PayoutRequestController extends BaseController { res.status(200).json({ pdf: pdf.downloadName } as PdfUrlResponse); } catch (error) { this.logger.error('Could get payout request PDF:', error); + if (error instanceof PdfError) { + res.status(502).json('PDF Generator service failed.'); + return; + } res.status(500).json('Internal server error.'); } } diff --git a/src/controller/seller-payout-controller.ts b/src/controller/seller-payout-controller.ts index b263ca5a4..ebbd33be1 100644 --- a/src/controller/seller-payout-controller.ts +++ b/src/controller/seller-payout-controller.ts @@ -26,6 +26,7 @@ import { PaginatedSellerPayoutResponse } from './response/seller-payout-response import { CreateSellerPayoutRequest, UpdateSellerPayoutRequest } from './request/seller-payout-request'; import User from '../entity/user/user'; import ReportService, { SalesReportService } from '../service/report-service'; +import { PdfError } from '../errors'; export default class SellerPayoutController extends BaseController { private logger: Logger = log4js.getLogger(' SellerPayoutController'); @@ -216,6 +217,10 @@ export default class SellerPayoutController extends BaseController { res.status(200).json({ pdf: pdf.downloadName }); } catch (error) { this.logger.error('Could not get sales report for seller payout:', error); + if (error instanceof PdfError) { + res.status(502).json('PDF Generator service failed.'); + return; + } res.status(500).json('Internal server error.'); } } diff --git a/src/controller/user-controller.ts b/src/controller/user-controller.ts index da75c6db6..e559090b1 100644 --- a/src/controller/user-controller.ts +++ b/src/controller/user-controller.ts @@ -61,6 +61,7 @@ import DebtorService from '../service/debtor-service'; import ReportService, { BuyerReportService, SalesReportService } from '../service/report-service'; import { ReturnFileType, UserReportParametersType } from 'pdf-generator-client'; import { reportPDFhelper } from '../helpers/express-pdf'; +import { PdfError } from '../errors'; export default class UserController extends BaseController { private logger: Logger = log4js.getLogger('UserController'); @@ -1181,6 +1182,10 @@ export default class UserController extends BaseController { await reportPDFhelper(res)(service, filters, description, user.id, UserReportParametersType.Sales, fileType); } catch (error) { this.logger.error('Could not get sales report:', error); + if (error instanceof PdfError) { + res.status(502).json('PDF Generator service failed.'); + return; + } res.status(500).json('Internal server error.'); } } @@ -1226,6 +1231,10 @@ export default class UserController extends BaseController { await (reportPDFhelper(res))(service, filters, description, user.id, UserReportParametersType.Purchases, fileType); } catch (error) { this.logger.error('Could not get sales report:', error); + if (error instanceof PdfError) { + res.status(502).json('PDF Generator service failed.'); + return; + } res.status(500).json('Internal server error.'); } } diff --git a/test/unit/controller/debtor-controller.ts b/test/unit/controller/debtor-controller.ts index af069e5af..f0182654e 100644 --- a/test/unit/controller/debtor-controller.ts +++ b/test/unit/controller/debtor-controller.ts @@ -632,7 +632,7 @@ describe('DebtorController', () => { .query({ fromDate, toDate, fileType: 'PDF' }); expect(res.status).to.equal(200); }); - it('should return 500 if pdf generation fails', async () => { + it('should return 502 if pdf generation fails', async () => { clientStub.generateFineReport.rejects(new Error('Failed to generate PDF')); const fromDate = new Date(); const toDate = new Date(fromDate.getTime() + 1000 * 60 * 60 * 24); @@ -640,7 +640,7 @@ describe('DebtorController', () => { .get('/fines/report/pdf') .set('Authorization', `Bearer ${ctx.adminToken}`) .query({ fromDate, toDate, fileType: 'PDF' }); - expect(res.status).to.equal(500); + expect(res.status).to.equal(502); }); it('should return 403 if not admin', async () => { const fromDate = new Date(); diff --git a/test/unit/controller/user-controller.ts b/test/unit/controller/user-controller.ts index 13941fb2d..81823cd6d 100644 --- a/test/unit/controller/user-controller.ts +++ b/test/unit/controller/user-controller.ts @@ -2300,7 +2300,7 @@ describe('UserController', (): void => { .query(parameters); expect(res.status).to.equal(200); }); - it('should return 500 if pdf generation fails', async () => { + it('should return 502 if pdf generation fails', async () => { clientStub.generateUserReport.rejects(new Error('Failed to generate PDF')); const id = 1; const parameters = { fromDate: '2021-01-01', tillDate: '2021-12-31' }; @@ -2310,7 +2310,7 @@ describe('UserController', (): void => { .get(`/users/${id}/transactions/sales/report/pdf`) .set('Authorization', `Bearer ${ctx.adminToken}`) .query(parameters); - expect(res.status).to.equal(500); + expect(res.status).to.equal(502); }); it('should return 403 if not admin', async () => { const id = 2; @@ -2393,7 +2393,7 @@ describe('UserController', (): void => { .query(parameters); expect(res.status).to.equal(200); }); - it('should return 500 if pdf generation fails', async () => { + it('should return 502 if pdf generation fails', async () => { clientStub.generateUserReport.rejects(new Error('Failed to generate PDF')); const id = 1; const parameters = { fromDate: '2021-01-01', tillDate: '2021-12-31' }; @@ -2403,7 +2403,7 @@ describe('UserController', (): void => { .get(`/users/${id}/transactions/purchases/report/pdf`) .set('Authorization', `Bearer ${ctx.adminToken}`) .query(parameters); - expect(res.status).to.equal(500); + expect(res.status).to.equal(502); }); it('should return 403 if not admin', async () => { const id = 2;