Skip to content

Commit

Permalink
feat: return 502 for PDF generator errors (#309)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3a8f937)
  • Loading branch information
JustSamuel committed Sep 11, 2024
1 parent 30ee49b commit ad3cdf4
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/controller/debtor-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/controller/invoice-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/controller/payout-request-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/controller/seller-payout-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/controller/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
}
}
Expand Down Expand Up @@ -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.');
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/controller/debtor-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,15 @@ 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);
const res = await request(ctx.app)
.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();
Expand Down
8 changes: 4 additions & 4 deletions test/unit/controller/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
Expand All @@ -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;
Expand Down Expand Up @@ -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' };
Expand All @@ -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;
Expand Down

0 comments on commit ad3cdf4

Please sign in to comment.