Skip to content

Commit

Permalink
Add a type predicate for CodedError, fixing a typescript error that…
Browse files Browse the repository at this point in the history
… appeared in `main` somehow. (#2110)
  • Loading branch information
filmaj committed May 6, 2024
1 parent 65413b9 commit f2f26bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/App-built-in-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sinon, { SinonSpy } from 'sinon';
import { assert } from 'chai';
import rewiremock from 'rewiremock';
import { Override, mergeOverrides, createFakeLogger, delay } from './test-helpers';
import { ErrorCode, UnknownError, AuthorizationError, CodedError } from './errors';
import { ErrorCode, UnknownError, AuthorizationError, CodedError, isCodedError } from './errors';
import {
Receiver,
ReceiverEvent,
Expand Down Expand Up @@ -354,9 +354,10 @@ describe('App built-in middleware and mechanism', () => {
// Assert
assert(fakeErrorHandler.calledOnce);
const error = fakeErrorHandler.firstCall.args[0];
assert.instanceOf(error, Error);
assert.ok(isCodedError(error));
assert(error.code === ErrorCode.MultipleListenerError);
assert.sameMembers(error.originals, errorsToThrow);
assert.isArray(error.originals);
if (error.originals) assert.sameMembers(error.originals, errorsToThrow);
});

it('should detect invalid event names', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface CodedError extends Error {
res?: ServerResponse; // HTTPReceiverDeferredRequestError
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isCodedError(err: any): err is CodedError {
return 'code' in err;
}

export enum ErrorCode {
AppInitializationError = 'slack_bolt_app_initialization_error',
AuthorizationError = 'slack_bolt_authorization_error',
Expand Down

0 comments on commit f2f26bd

Please sign in to comment.