From afe6e0ba849feecad04d4ca2f6ae37af32c620ea Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 25 Aug 2022 14:40:19 +0100 Subject: [PATCH] fix: enforce prototypes in converter-failures Typescript sometimes decides to "compile out" prototype chains like this we have to *enforce* that these classes have the right inheritance, because we do instanceof checks to determine what kind of error was thrown. We could switch to a tagged union approach, but this seems more stable. --- .../framework/common/converter-failures.ts | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/server/src/lib/score-import/framework/common/converter-failures.ts b/server/src/lib/score-import/framework/common/converter-failures.ts index de7f440b2..02985fa03 100644 --- a/server/src/lib/score-import/framework/common/converter-failures.ts +++ b/server/src/lib/score-import/framework/common/converter-failures.ts @@ -9,6 +9,8 @@ export class ConverterFailure extends Error { constructor(message: string) { super(); this.message = message; + + Object.setPrototypeOf(this, ConverterFailure); } } @@ -17,7 +19,18 @@ export class ConverterFailure extends Error { * support it. This is not an error, but is not a success either. * An example scenario would be something like a 5KEY score being imported from SSS. */ -export class SkipScoreFailure extends ConverterFailure {} +export class SkipScoreFailure extends ConverterFailure { + constructor(message: string) { + super(message); + + // @hack + // Typescript sometimes decides to "compile out" prototype chains like this + // we have to *enforce* that these classes have the right inheritance, + // because we do instanceof checks to determine what kind of error was + // thrown. We could switch to a tagged union approach, but this seems more stable. + Object.setPrototypeOf(this, SkipScoreFailure); + } +} /** * KTDataNotFoundError - We could not find the song or chart this score @@ -40,6 +53,8 @@ export class KTDataNotFoundFailure extends ConverterFailu this.importType = importType; this.data = data; this.converterContext = context; + + Object.setPrototypeOf(this, KTDataNotFoundFailure); } } @@ -47,10 +62,22 @@ export class KTDataNotFoundFailure extends ConverterFailu * InvalidScoreError - This score provided invalid data that we * can not accept. */ -export class InvalidScoreFailure extends ConverterFailure {} +export class InvalidScoreFailure extends ConverterFailure { + constructor(message: string) { + super(message); + + Object.setPrototypeOf(this, InvalidScoreFailure); + } +} /** * KTInternalFailure - An unexpected, internal error has occured, * and the score could not be processed. */ -export class InternalFailure extends ConverterFailure {} +export class InternalFailure extends ConverterFailure { + constructor(message: string) { + super(message); + + Object.setPrototypeOf(this, InternalFailure); + } +}