Skip to content

Commit

Permalink
Fix #925 by adding optional properties to CodedError interface (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed May 19, 2021
1 parent 2c1a1e9 commit 5185d7b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import type { BufferedIncomingMessage } from './receivers/verify-request';
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
export interface CodedError extends Error {
code: string; // This can be a value from ErrorCode, or WebClient's ErrorCode, or a NodeJS error code
original?: Error; // AuthorizationError, UnknownError
originals?: Error[]; // MultipleListenerError
missingProperty?: string; // ContextMissingPropertyError
req?: IncomingMessage | BufferedIncomingMessage; // HTTPReceiverDeferredRequestError
res?: ServerResponse; // HTTPReceiverDeferredRequestError
}

export enum ErrorCode {
Expand Down
43 changes: 43 additions & 0 deletions types-tests/error.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import App from '../src/App';
import { expectType } from 'tsd';
import { CodedError } from '../src/errors';
import { IncomingMessage, ServerResponse } from 'http';
import { BufferedIncomingMessage } from '../src/receivers/verify-request';

const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' });

// https://github.com/slackapi/bolt-js/issues/925
// CodedError should have original and so on
app.error(async (error) => {
expectType<CodedError>(error);

expectType<Error | undefined>(error.original);
if (error.original != undefined) {
expectType<Error>(error.original);
console.log(error.original.message);
}

expectType<Error[] | undefined>(error.originals);
if (error.originals != undefined) {
expectType<Error[]>(error.originals);
console.log(error.originals);
}

expectType<string | undefined>(error.missingProperty);
if (error.missingProperty != undefined) {
expectType<string>(error.missingProperty);
console.log(error.missingProperty);
}

expectType<IncomingMessage | BufferedIncomingMessage | undefined>(error.req);
if (error.req != undefined) {
expectType<IncomingMessage | BufferedIncomingMessage>(error.req);
console.log(error.req);
}

expectType<ServerResponse | undefined>(error.res);
if (error.res != undefined) {
expectType<ServerResponse>(error.res);
console.log(error.res);
}
});

0 comments on commit 5185d7b

Please sign in to comment.