MCPcopy
hub / github.com/bbc/sqs-consumer / executeHandler

Method executeHandler

src/consumer.ts:539–598  ·  view source on GitHub ↗

* Trigger the applications handleMessage function * @param message The message that was received from SQS

(message: Message)

Source from the content-addressed store, hash-verified

537 * @param message The message that was received from SQS
538 */
539 private async executeHandler(message: Message): Promise<Message> {
540 let handleMessageTimeoutId: NodeJS.Timeout | undefined = undefined;
541
542 try {
543 let result: Message | undefined | null;
544
545 if (this.handleMessageTimeout) {
546 const pending: Promise<never> = new Promise<never>((_, reject) => {
547 handleMessageTimeoutId = setTimeout(() => {
548 reject(new TimeoutError());
549 }, this.handleMessageTimeout);
550 });
551 result = await Promise.race([this.handleMessage(message), pending]);
552 } else {
553 result = await this.handleMessage(message);
554 }
555
556 if (this.alwaysAcknowledge) {
557 return message;
558 }
559
560 if (result instanceof Object) {
561 return result;
562 }
563
564 if (result === undefined) {
565 return null;
566 }
567
568 if (result === null) {
569 if (this.strictReturn) {
570 throw new Error(
571 "strictReturn is enabled: handleMessage must return a Message object or an object with the same MessageId. Returning null is not allowed.",
572 );
573 }
574 console.warn(
575 "[DEPRECATION] Future versions will throw on void/null returns. Enable `strictReturn` now to prepare.",
576 );
577 return null;
578 }
579
580 return null;
581 } catch (err) {
582 if (err instanceof TimeoutError) {
583 throw toTimeoutError(
584 err,
585 `Message handler timed out after ${this.handleMessageTimeout}ms: Operation timed out.`,
586 message,
587 );
588 }
589 if (err instanceof Error) {
590 throw toStandardError(err, `Unexpected message handler failure: ${err.message}`, message);
591 }
592 throw err;
593 } finally {
594 if (handleMessageTimeoutId) {
595 clearTimeout(handleMessageTimeoutId);
596 }

Callers 1

processMessageMethod · 0.95

Calls 3

toTimeoutErrorFunction · 0.85
toStandardErrorFunction · 0.85
handleMessageMethod · 0.80

Tested by

no test coverage detected