Skip to content

Commit

Permalink
better exception handling and typing (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnataylor authored Dec 6, 2019
1 parent 867a599 commit fe85099
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
10 changes: 10 additions & 0 deletions libraries/botbuilder/src/botFrameworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ export interface WebRequest {
*/
method?: any;

/***
* Optional. The request parameters from the url.
*/
params?: any;

/***
* Optional. The values from the query string.
*/
query?: any;

/**
* When implemented in a derived class, adds a listener for an event.
* The framework uses this method to retrieve the request body when the
Expand Down
60 changes: 33 additions & 27 deletions libraries/botbuilder/src/channelServiceRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { ChannelServiceHandler } from './channelServiceHandler';
import { Activity, ConversationParameters, Transcript, AttachmentData } from 'botbuilder-core';
import { WebRequest, WebResponse, StatusCodeError } from './botFrameworkAdapter';
import { WebRequest, WebResponse, StatusCodeError, StatusCodes } from './botFrameworkAdapter';

export class ChannelServiceRoutes {

Expand All @@ -31,7 +31,7 @@ export class ChannelServiceRoutes {
server.post(baseAddress + '/v3/conversations/:conversationId/attachments', this.processUploadAttachment.bind(this));
}

processSendToConversation(req, res) {
processSendToConversation(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
ChannelServiceRoutes.readActivity(req)
.then((activity) => {
Expand All @@ -44,10 +44,11 @@ export class ChannelServiceRoutes {
res.end();
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); })
});
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processReplyToActivity(req, res) {
processReplyToActivity(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
ChannelServiceRoutes.readActivity(req)
.then((activity) => {
Expand All @@ -60,10 +61,11 @@ export class ChannelServiceRoutes {
res.end();
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); })
});
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processUpdateActivity(req, res) {
processUpdateActivity(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
ChannelServiceRoutes.readActivity(req)
.then((activity) => {
Expand All @@ -76,20 +78,21 @@ export class ChannelServiceRoutes {
res.end();
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); })
});
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processDeleteActivity(req, res) {
processDeleteActivity(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
this.channelServiceHandler.handleDeleteActivity(authHeader, req.params.conversationId, req.params.activityId)
.then(() => {
res.status(200);
res.end();
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}
}

processGetActivityMembers(req, res) {
processGetActivityMembers(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
this.channelServiceHandler.handleGetActivityMembers(authHeader, req.params.conversationId, req.params.activityId)
.then((channelAccounts) => {
Expand All @@ -102,7 +105,7 @@ export class ChannelServiceRoutes {
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processCreateConversation(req, res) {
processCreateConversation(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
ChannelServiceRoutes.readBody<ConversationParameters>(req)
.then((conversationParameters) => {
Expand All @@ -118,7 +121,7 @@ export class ChannelServiceRoutes {
});
}

processGetConversations(req, res) {
processGetConversations(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
this.channelServiceHandler.handleGetConversations(authHeader, req.params.conversationId, req.query.continuationToken)
.then((conversationsResult) => {
Expand All @@ -131,7 +134,7 @@ export class ChannelServiceRoutes {
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processGetConversationMembers(req, res) {
processGetConversationMembers(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
this.channelServiceHandler.handleGetConversationMembers(authHeader, req.params.conversationId)
.then((channelAccounts) => {
Expand All @@ -144,7 +147,7 @@ export class ChannelServiceRoutes {
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processGetConversationPagedMembers(req, res) {
processGetConversationPagedMembers(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
let pageSize = parseInt(req.query.pageSize);
if (isNaN(pageSize))
Expand All @@ -166,7 +169,7 @@ export class ChannelServiceRoutes {
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processDeleteConversationMember(req, res) {
processDeleteConversationMember(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
this.channelServiceHandler.handleDeleteConversationMember(authHeader, req.params.conversationId, req.params.memberId)
.then((resourceResponse) => {
Expand All @@ -176,7 +179,7 @@ export class ChannelServiceRoutes {
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processSendConversationHistory(req, res) {
processSendConversationHistory(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
ChannelServiceRoutes.readBody<Transcript>(req)
.then((transcript) => {
Expand All @@ -185,14 +188,15 @@ export class ChannelServiceRoutes {
if (resourceResponse) {
res.send(resourceResponse);
}
res.status(201);
res.status(200);
res.end();
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); })
});
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

processUploadAttachment(req, res) {
processUploadAttachment(req: WebRequest, res: WebResponse) {
const authHeader = req.headers.authorization || req.headers.Authorization || '';
ChannelServiceRoutes.readBody<AttachmentData>(req)
.then((attachmentData) => {
Expand All @@ -201,18 +205,19 @@ export class ChannelServiceRoutes {
if (resourceResponse) {
res.send(resourceResponse);
}
res.status(201);
res.status(200);
res.end();
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); })
});
})
.catch(err => { ChannelServiceRoutes.handleError(err, res); });
}

static readActivity(req: WebRequest) : Promise<Activity> {
return new Promise((resolve, reject) => {
function returnActivity(activity) {
if (typeof activity !== 'object') { throw new Error(`BotFrameworkAdapter.parseRequest(): invalid request body.`); }
if (typeof activity.type !== 'string') { throw new Error(`BotFrameworkAdapter.parseRequest(): missing activity type.`); }
if (typeof activity !== 'object') { throw new Error(`Invalid request body.`); }
if (typeof activity.type !== 'string') { throw new Error(`Missing activity type.`); }
if (typeof activity.timestamp === 'string') { activity.timestamp = new Date(activity.timestamp); }
if (typeof activity.localTimestamp === 'string') { activity.localTimestamp = new Date(activity.localTimestamp); }
if (typeof activity.expiration === 'string') { activity.expiration = new Date(activity.expiration); }
Expand All @@ -223,7 +228,7 @@ export class ChannelServiceRoutes {
try {
returnActivity(req.body);
} catch (err) {
reject(err);
reject(new StatusCodeError(StatusCodes.BAD_REQUEST, err.message));
}
} else {
let requestData = '';
Expand All @@ -235,7 +240,7 @@ export class ChannelServiceRoutes {
const body = JSON.parse(requestData);
returnActivity(body);
} catch (err) {
reject(err);
reject(new StatusCodeError(StatusCodes.BAD_REQUEST, err.message));
}
});
}
Expand All @@ -248,7 +253,7 @@ export class ChannelServiceRoutes {
try {
resolve(req.body);
} catch (err) {
reject(err);
reject(new StatusCodeError(StatusCodes.BAD_REQUEST, err.message));
}
} else {
let requestData = '';
Expand All @@ -260,7 +265,7 @@ export class ChannelServiceRoutes {
const body = JSON.parse(requestData);
resolve(body);
} catch (err) {
reject(err);
reject(new StatusCodeError(StatusCodes.BAD_REQUEST, err.message));
}
});
}
Expand All @@ -269,6 +274,7 @@ export class ChannelServiceRoutes {

static handleError(err: any, res: WebResponse) {
if (err instanceof StatusCodeError) {
res.send(err.message);
res.status(err.statusCode);
} else {
res.status(500);
Expand Down

0 comments on commit fe85099

Please sign in to comment.