Skip to content

Commit

Permalink
Fix #859 Improve the way to handle authorize fn errors in built-in re…
Browse files Browse the repository at this point in the history
…ceivers
  • Loading branch information
seratch committed Aug 27, 2021
1 parent 926b669 commit d641204
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
WorkflowStepEdit,
} from './types';
import { IncomingEventType, getTypeAndConversation, assertNever } from './helpers';
import { CodedError, asCodedError, AppInitializationError, MultipleListenerError } from './errors';
import { CodedError, asCodedError, AppInitializationError, MultipleListenerError, ErrorCode } from './errors';
// eslint-disable-next-line import/order
import allSettled = require('promise.allsettled'); // eslint-disable-line @typescript-eslint/no-require-imports
// eslint-disable-next-line @typescript-eslint/no-require-imports, import/no-commonjs
Expand Down Expand Up @@ -692,7 +692,7 @@ export default class App {
} catch (error) {
const e = error as any;
this.logger.warn('Authorization of incoming event did not succeed. No listeners will be called.');
e.code = 'slack_bolt_authorization_error';
e.code = ErrorCode.AuthorizationError;
// disabling due to https://github.com/typescript-eslint/typescript-eslint/issues/1277
// eslint-disable-next-line consistent-return
return this.handleError(e);
Expand Down Expand Up @@ -907,7 +907,7 @@ export default class App {
}

function defaultErrorHandler(logger: Logger): ErrorHandler {
return (error) => {
return (error: CodedError) => {
logger.error(error);

return Promise.reject(error);
Expand Down
19 changes: 18 additions & 1 deletion src/receivers/ExpressReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import tsscmp from 'tsscmp';
import { Logger, ConsoleLogger, LogLevel } from '@slack/logger';
import { InstallProvider, CallbackOptions, InstallProviderOptions, InstallURLOptions } from '@slack/oauth';
import App from '../App';
import { ReceiverAuthenticityError, ReceiverMultipleAckError, ReceiverInconsistentStateError } from '../errors';
import {
ReceiverAuthenticityError,
ReceiverMultipleAckError,
ReceiverInconsistentStateError,
ErrorCode,
CodedError,
} from '../errors';
import { AnyMiddlewareArgs, Receiver, ReceiverEvent } from '../types';
import renderHtmlForInstallPath from './render-html-for-install-path';

Expand Down Expand Up @@ -263,6 +269,17 @@ export default class ExpressReceiver implements Receiver {
this.logger.debug('stored response sent');
}
} catch (err) {
const e = err as any;
if ('code' in e) {
// CodedError has code: string
const errorCode = (err as CodedError).code;
if (errorCode === ErrorCode.AuthorizationError) {
// authorize function threw an exception, which means there is no valid installation data
res.status(401).send();
isAcknowledged = true;
return;
}
}
res.status(500).send();
throw err;
}
Expand Down
12 changes: 12 additions & 0 deletions src/receivers/HTTPReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ReceiverInconsistentStateError,
HTTPReceiverDeferredRequestError,
ErrorCode,
CodedError,
} from '../errors';

// Option keys for tls.createServer() and tls.createSecureContext(), exclusive of those for http.createServer()
Expand Down Expand Up @@ -406,6 +407,17 @@ export default class HTTPReceiver implements Receiver {
}
} catch (err) {
const e = err as any;
if ('code' in e) {
// CodedError has code: string
const errorCode = (e as CodedError).code;
if (errorCode === ErrorCode.AuthorizationError) {
// authorize function threw an exception, which means there is no valid installation data
res.writeHead(401);
res.end();
isAcknowledged = true;
return;
}
}
this.logger.error('An unhandled error occurred while Bolt processed an event');
this.logger.debug(`Error details: ${e}, storedResponse: ${storedResponse}`);
res.writeHead(500);
Expand Down

0 comments on commit d641204

Please sign in to comment.