Skip to content

Commit

Permalink
fix: Send error data properly to CatchDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
windkomo committed Aug 2, 2018
1 parent 58a94e2 commit a0956d3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
23 changes: 20 additions & 3 deletions packages/botfuel-dialog/src/bot.js
Expand Up @@ -19,8 +19,11 @@
import type { Config, RawConfig } from './config';
import type {
UserMessage,
PostbackMessage, ImageMessage, TextMessage,
PostbackMessage,
ImageMessage,
TextMessage,
DialogData,
ErrorObject,
} from './types';
import type { BotMessageJson } from './messages/message';
import type Adapter from './adapters/adapter';
Expand Down Expand Up @@ -211,8 +214,9 @@ class Bot {
return this.dm.executeDialog(userMessage, dialog);
}

async respondWhenError(userMessage: UserMessage, error: Error): Promise<BotMessageJson[]> {
async respondWhenError(userMessage: UserMessage, error: ErrorObject): Promise<BotMessageJson[]> {
logger.debug('respondWhenError', userMessage, error);

if (error instanceof AuthenticationError) {
logger.error('Botfuel API authentication failed!');
logger.error(
Expand All @@ -223,12 +227,25 @@ class Bot {
} else if (error instanceof DialogError) {
logger.error(`Could not execute dialog '${error.name}'`);
}

const keys = Object.getOwnPropertyNames(error);
// error is not a standard JS Object so we have to copy each property
// one by one
const errorObject = keys.reduce(
(obj, key) => ({
...obj,
[key]: error[key],
}),
{},
);

const catchDialog = {
name: 'catch',
data: {
error,
error: errorObject,
},
};

return this.dm.executeDialog(userMessage, catchDialog);
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/botfuel-dialog/src/types.js
Expand Up @@ -2,6 +2,12 @@

import type ClassificationResult, { QnaAnswers } from './nlus/classification-result';

/*
Error types
*/

export type ErrorObject = { [key: string]: any };

/*
Data used in the dialogs
*/
Expand All @@ -13,9 +19,9 @@ export type DialogDataData = {
messageEntities?: MessageEntities,
answers?: QnaAnswers,
url?: string,
error?: ErrorObject,
};


export type DialogData = {
name: string,
data: DialogDataData,
Expand Down Expand Up @@ -44,7 +50,6 @@ export type UserData = {
_createdAt: number,
};


/*
Message types
*/
Expand Down
29 changes: 29 additions & 0 deletions packages/test-complexdialogs/src/views/catch-view.js
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2017 - present, Botfuel (https://www.botfuel.io).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const { View, BotTextMessage } = require('botfuel-dialog');

class CatchView extends View {
render(userMessage, { error }) {
return [
new BotTextMessage('The following error occured:'),
new BotTextMessage(`name: ${error.name}, message: ${error.message}`),
new BotTextMessage('Starting a new conversation...'),
];
}
}

module.exports = CatchView;
2 changes: 1 addition & 1 deletion packages/test-complexdialogs/tests/error.test.js
Expand Up @@ -28,7 +28,7 @@ describe('ErrorDialog', () => {
[
new UserTextMessage('error'),
new BotTextMessage('The following error occured:'),
new BotTextMessage('{"name":"SdkError"}'),
new BotTextMessage('name: SdkError, message: bla'),
new BotTextMessage('Starting a new conversation...'),
].map(msg => msg.toJson(userId)),
);
Expand Down

0 comments on commit a0956d3

Please sign in to comment.