Skip to content

Commit 5816400

Browse files
committed
🥅 Catch errors in controller
1 parent 278c5c7 commit 5816400

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

src/models/controller.model.ts

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ export class Controller {
407407
): Promise<void> {
408408
const { providerConfig } = req;
409409

410-
if (!providerConfig) {
411-
throw new ServerError(400, 'Missing parameters');
412-
}
413-
414410
try {
411+
if (!providerConfig) {
412+
throw new ServerError(400, 'Missing parameters');
413+
}
414+
415415
infoLogger('getContactsDelta', 'START', providerConfig.apiKey);
416416

417417
const fetchContactsDelta = async (): Promise<ContactDelta> => {
@@ -461,7 +461,7 @@ export class Controller {
461461
errorLogger(
462462
'getContacts',
463463
'Could not get contacts:',
464-
providerConfig.apiKey,
464+
providerConfig?.apiKey,
465465
error,
466466
);
467467
next(error);
@@ -474,26 +474,25 @@ export class Controller {
474474
next: NextFunction,
475475
): Promise<void> {
476476
const { providerConfig } = req;
477+
try {
478+
if (!providerConfig) {
479+
throw new ServerError(400, 'Missing parameters');
480+
}
477481

478-
if (!providerConfig) {
479-
throw new ServerError(400, 'Missing parameters');
480-
}
481-
482-
if (!this.adapter.getContact) {
483-
throw new ServerError(501, 'Getting single contact is not implemented');
484-
}
482+
if (!this.adapter.getContact) {
483+
throw new ServerError(501, 'Getting single contact is not implemented');
484+
}
485485

486-
infoLogger('getContact', 'START', providerConfig.apiKey);
486+
infoLogger('getContact', 'START', providerConfig.apiKey);
487487

488-
const contactType: IntegrationEntityType | undefined = Object.values(
489-
IntegrationEntityType,
490-
).find((value) => value === req.query.type?.toString());
488+
const contactType: IntegrationEntityType | undefined = Object.values(
489+
IntegrationEntityType,
490+
).find((value) => value === req.query.type?.toString());
491491

492-
if (!contactType) {
493-
throw new ServerError(400, 'Missing contact type query parameter');
494-
}
492+
if (!contactType) {
493+
throw new ServerError(400, 'Missing contact type query parameter');
494+
}
495495

496-
try {
497496
const contactId = req.params.id;
498497
infoLogger(
499498
'getContact',
@@ -522,7 +521,7 @@ export class Controller {
522521
errorLogger(
523522
'getContact',
524523
'Could not get contact:',
525-
providerConfig.apiKey,
524+
providerConfig?.apiKey,
526525
error,
527526
);
528527
next(error);
@@ -1364,17 +1363,18 @@ export class Controller {
13641363
next: NextFunction,
13651364
): Promise<void> {
13661365
const { providerConfig } = req;
1367-
if (!providerConfig) {
1368-
throw new ServerError(400, 'Missing parameters');
1369-
}
13701366

1371-
if (!this.adapter.getAccountId) {
1372-
throw new ServerError(501, 'Fetching account id is not implemented');
1373-
}
1367+
try {
1368+
if (!providerConfig) {
1369+
throw new ServerError(400, 'Missing parameters');
1370+
}
13741371

1375-
infoLogger('getAccountId', 'START', providerConfig.apiKey);
1372+
if (!this.adapter.getAccountId) {
1373+
throw new ServerError(501, 'Fetching account id is not implemented');
1374+
}
1375+
1376+
infoLogger('getAccountId', 'START', providerConfig.apiKey);
13761377

1377-
try {
13781378
const accountId = await this.adapter.getAccountId(providerConfig);
13791379

13801380
if (!accountId) {
@@ -1399,36 +1399,24 @@ export class Controller {
13991399
res: Response,
14001400
next: NextFunction,
14011401
): Promise<void> {
1402-
if (!this.adapter.handleWebhook) {
1403-
throw new ServerError(501, 'Webhook handling not implemented');
1404-
}
1405-
1406-
if (!this.adapter.verifyWebhookRequest) {
1407-
throw new ServerError(501, 'Webhook verification not implemented');
1408-
}
1402+
try {
1403+
if (!this.adapter.handleWebhook) {
1404+
throw new ServerError(501, 'Webhook handling not implemented');
1405+
}
14091406

1410-
let verified: boolean;
1407+
if (!this.adapter.verifyWebhookRequest) {
1408+
throw new ServerError(501, 'Webhook verification not implemented');
1409+
}
14111410

1412-
try {
1413-
verified = await this.adapter.verifyWebhookRequest(req);
1414-
} catch (error) {
1415-
errorLogger(
1416-
'handleWebhook',
1417-
'Error while verifying webhook request:',
1418-
'',
1419-
error || 'Unknown',
1420-
);
1421-
throw new ServerError(403, 'Webhook verification failed');
1422-
}
1411+
const verified = await this.adapter.verifyWebhookRequest(req);
14231412

1424-
if (!verified) {
1425-
errorLogger('handleWebhook', 'Webhook verification failed', '');
1426-
throw new ServerError(403, 'Webhook verification failed');
1427-
}
1413+
if (!verified) {
1414+
errorLogger('handleWebhook', 'Webhook verification failed', '');
1415+
throw new ServerError(403, 'Webhook verification failed');
1416+
}
14281417

1429-
infoLogger('handleWebhook', 'START', '');
1418+
infoLogger('handleWebhook', 'START', '');
14301419

1431-
try {
14321420
const changeEvents: ContactChangeEvent[] =
14331421
await this.adapter.handleWebhook(req);
14341422

0 commit comments

Comments
 (0)