From 50c661be15dc7a485fdb5c9fb9d84431d7d7705f Mon Sep 17 00:00:00 2001 From: Mark Doliner Date: Mon, 1 Jun 2020 11:14:45 -0400 Subject: [PATCH] Add a StructLike class that extends Error. Exception classes should extend this so that they derive from Error. As mentioned in [the thrift-typescript GitHub issue](https://github.com/creditkarma/thrift-typescript/issues/178), it's useful for exceptions to derive from Error so they have stack traces and because some frameworks expect it. The GitHub issue mentions graphql-js. Jest's `toThrow()` function would also benefit from this. See the related thrift-typescript PR for more details. Possible remaining work: - Add a test? - Bump version number so end users can require the new version. --- packages/thrift-server-core/src/main/types.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/thrift-server-core/src/main/types.ts b/packages/thrift-server-core/src/main/types.ts index e10aa291..b2efb530 100644 --- a/packages/thrift-server-core/src/main/types.ts +++ b/packages/thrift-server-core/src/main/types.ts @@ -86,6 +86,22 @@ export abstract class StructLike implements IStructLike { public abstract write(output: TProtocol): void } +/** + * Like `StructLike` but extends `Error`. Exception classes extend this. + */ +export abstract class ErrorStructLike extends Error implements IStructLike { + public readonly name: string + public readonly _annotations: IThriftAnnotations = {} + public readonly _fieldAnnotations: IFieldAnnotations = {} + + constructor() { + super() + this.name = this.constructor.name + } + + public abstract write(output: TProtocol): void +} + export interface IStructConstructor { new (args?: any): T read(input: TProtocol): T