Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions sdks/typescript/pmxt/hosted-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* does not support multiple inheritance, so each hosted error class extends
* the semantically-closest legacy parent (e.g. `InsufficientEscrowBalance`
* extends `InsufficientFunds`) so existing `instanceof` checks continue to
* match. The `static isHostedError = true` flag and the {@link isHostedError}
* helper provide a structural alternative for callers that want to detect
* hosted-mode failures regardless of the concrete subclass.
* match. `HostedTradingError[Symbol.hasInstance]` uses the `static
* isHostedError = true` flag so callers can also catch any hosted-mode failure
* with `e instanceof HostedTradingError`.
*/

import {
Expand All @@ -25,6 +25,14 @@ export class HostedTradingError extends PmxtError {
readonly status: number;
readonly detail: string;

static [Symbol.hasInstance](value: unknown): boolean {
if (this !== HostedTradingError) {
return Function.prototype[Symbol.hasInstance].call(this, value);
}
const ctor = (value as { constructor?: { isHostedError?: boolean } } | null)?.constructor;
return ctor != null && ctor.isHostedError === true;
}

constructor(status: number, detail: string) {
super(detail);
this.status = status;
Expand Down
14 changes: 14 additions & 0 deletions sdks/typescript/tests/hosted-error-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ describe("isHostedError flag", () => {
expect(isHostedError(err)).toBe(true);
});

it.each([
["HostedTradingError", new HostedTradingError(500, "x")],
["InvalidApiKey", new InvalidApiKey(401, "x")],
["InsufficientEscrowBalance", new InsufficientEscrowBalance(403, "x")],
["OrderSizeTooSmall", new OrderSizeTooSmall(422, "x")],
["OutcomeNotFound", new OutcomeNotFound(404, "x")],
["CatalogUnavailable", new CatalogUnavailable(503, "x")],
["BuiltOrderExpired", new BuiltOrderExpired(410, "x")],
["InvalidSignature", new InvalidSignature(422, "x")],
["NoLiquidity", new NoLiquidity(422, "x")],
])("%s is catchable as HostedTradingError", (_label, err) => {
expect(err).toBeInstanceOf(HostedTradingError);
});

it("MissingWalletAddress is NOT a hosted error (local-only)", () => {
expect(isHostedError(new MissingWalletAddress("x"))).toBe(false);
});
Expand Down
Loading