Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Fix unhandled promise rejections when a trigger errors #1983

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions packages/botkit/src/botworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,9 @@ export class BotWorker {
* @returns Return value will contain the results of the send action, typically `{id: <id of message>}`
*/
public async say(message: Partial<BotkitMessage> | string): Promise<any> {
return new Promise((resolve, reject) => {
const activity = this.ensureMessageFormat(message);

this._controller.middleware.send.run(this, activity, async (err, bot, activity) => {
if (err) {
return reject(err);
}
resolve(await this.getConfig('context').sendActivity(activity));
});
});
const activity = this.ensureMessageFormat(message);
await this._controller.middleware.send.run(this, activity);
return this.getConfig('context').sendActivity(activity)
};

/**
Expand Down
16 changes: 4 additions & 12 deletions packages/botkit/src/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,18 +894,10 @@ export class BotkitConversation<O extends object = {}> extends Dialog<O> {
outgoing.suggestedActions = this.parseTemplatesRecursive(outgoing.suggestedActions, vars);
}

return new Promise((resolve, reject) => {
// run the outgoing message through the Botkit send middleware
this._controller.spawn(dc).then((bot) => {
this._controller.middleware.send.run(bot, outgoing, (err, bot, outgoing) => {
if (err) {
reject(err);
} else {
resolve(outgoing);
}
});
}).catch(reject);
});
const bot = await this._controller.spawn(dc);
await this._controller.middleware.send.run(bot, outgoing);

return outgoing;
}

/**
Expand Down
83 changes: 32 additions & 51 deletions packages/botkit/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as bodyParser from 'body-parser';
import * as Ware from 'ware';
import * as fs from 'fs';
import * as Debug from 'debug';
import * as util from 'util';

const debug = Debug('botkit');

Expand Down Expand Up @@ -367,6 +368,12 @@ export class Botkit {

this.dialogSet = new DialogSet(dialogState);

this.middleware.spawn.run = util.promisify(this.middleware.spawn.run).bind( this.middleware.spawn);
this.middleware.ingest.run = util.promisify(this.middleware.ingest.run).bind( this.middleware.ingest);
this.middleware.send.run = util.promisify(this.middleware.send.run).bind( this.middleware.send);
this.middleware.receive.run = util.promisify(this.middleware.receive.run).bind( this.middleware.receive);
this.middleware.interpret.run = util.promisify(this.middleware.interpret.run).bind( this.middleware.interpret);

if (this._config.disable_webserver !== true) {
if (!this._config.webserver) {
// Create HTTP server
Expand Down Expand Up @@ -746,33 +753,21 @@ export class Botkit {
// Spawn a bot worker with the dialogContext
const bot = await this.spawn(dialogContext);

return new Promise((resolve, reject) => {
this.middleware.ingest.run(bot, message, async (err, bot, message) => {
if (err) {
reject(err);
} else {
this.middleware.receive.run(bot, message, async (err, bot, message) => {
if (err) {
reject(err);
} else {
const interrupt_results = await this.listenForInterrupts(bot, message);

if (interrupt_results === false) {
// Continue dialog if one is present
const dialog_results = await dialogContext.continueDialog();
if (dialog_results && dialog_results.status === DialogTurnStatus.empty) {
await this.processTriggersAndEvents(bot, message);
}
}

// make sure changes to the state get persisted after the turn is over.
await this.saveState(bot);
resolve();
}
});
}
});
});
await this.middleware.ingest.run(bot, message);
await this.middleware.receive.run(bot, message);

const interrupt_results = await this.listenForInterrupts(bot, message);

if (interrupt_results === false) {
// Continue dialog if one is present
const dialog_results = await dialogContext.continueDialog();
if (dialog_results && dialog_results.status === DialogTurnStatus.empty) {
await this.processTriggersAndEvents(bot, message);
}
}

// make sure changes to the state get persisted after the turn is over.
await this.saveState(bot);
}

/**
Expand All @@ -791,23 +786,16 @@ export class Botkit {
* @param message {BotkitMessage} an incoming message
*/
private async processTriggersAndEvents(bot: BotWorker, message: BotkitMessage): Promise<any> {
return new Promise((resolve, reject) => {
this.middleware.interpret.run(bot, message, async (err, bot, message) => {
if (err) {
return reject(err);
}
const listen_results = await this.listenForTriggers(bot, message);
await this.middleware.interpret.run(bot, message);

if (listen_results !== false) {
resolve(listen_results);
} else {
// Trigger event handlers
const trigger_results = await this.trigger(message.type, bot, message);
const listen_results = await this.listenForTriggers(bot, message);

resolve(trigger_results);
}
});
});
if (listen_results !== false) {
return listen_results;
}

// Trigger event handlers
return this.trigger(message.type, bot, message);
}

/**
Expand Down Expand Up @@ -1102,15 +1090,8 @@ export class Botkit {
// make sure the adapter is available in a standard location.
worker.getConfig().adapter = adapter;

return new Promise((resolve, reject) => {
this.middleware.spawn.run(worker, (err, worker) => {
if (err) {
reject(err);
} else {
resolve(worker);
}
});
});
await this.middleware.spawn.run(worker);
return worker;
}

/**
Expand Down