From 3957edb74fcf28ef4b6b983281dd80bba8fcd63c Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Tue, 30 Apr 2024 11:04:29 +0100 Subject: [PATCH] Overhaul printing of flagged values --- grafast/grafast/src/error.ts | 21 +++++++++++++++++++++ grafast/grafast/src/steps/__flag.ts | 11 ++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/grafast/grafast/src/error.ts b/grafast/grafast/src/error.ts index f18c48cb70..03b8607b24 100644 --- a/grafast/grafast/src/error.ts +++ b/grafast/grafast/src/error.ts @@ -1,3 +1,4 @@ +import { inspect } from "./inspect.js"; import type { ExecutionEntryFlags } from "./interfaces.js"; import { $$safeError, @@ -18,6 +19,17 @@ export interface FlaggedValue { flags: ExecutionEntryFlags; value: TValue; planId: number | null; + toString(): string; +} + +function flaggedValueToString(this: FlaggedValue) { + if (this.flags & FLAG_ERROR && this.value instanceof Error) { + return String(this.value); + } else if (this.flags & FLAG_INHIBITED && this.value === null) { + return "INHIBIT"; + } else { + return `${this.flags}/${inspect(this.value)}`; + } } function flaggedValue( @@ -25,11 +37,20 @@ function flaggedValue( value: any, planId: null | number, ): FlaggedValue { + if (value === null && !(flags & FLAG_NULL)) { + throw new Error(`flaggedValue called with null, but not flagged as null.`); + } + if (value === null && !(flags & FLAG_INHIBITED)) { + throw new Error( + `flaggedValue called with null, but not flagged as inhibited.`, + ); + } return { [$$flagged]: true, flags, value, planId, + toString: flaggedValueToString, }; } diff --git a/grafast/grafast/src/steps/__flag.ts b/grafast/grafast/src/steps/__flag.ts index 3730aa4e4e..8fb06895c4 100644 --- a/grafast/grafast/src/steps/__flag.ts +++ b/grafast/grafast/src/steps/__flag.ts @@ -1,5 +1,5 @@ import type { FlaggedValue } from "../error.js"; -import { $$inhibit, flagError, SafeError } from "../error.js"; +import { $$inhibit, flagError, isFlaggedValue, SafeError } from "../error.js"; import { inspect } from "../inspect.js"; import type { AddDependencyOptions, @@ -138,12 +138,9 @@ export class __FlagStep extends ExecutableStep { } public toStringMeta(): string | null { const acceptFlags = ALL_FLAGS & ~this.forbiddenFlags; - const rej = - this.onRejectReturnValue === $$inhibit - ? `INHIBIT` - : this.onRejectReturnValue - ? trim(String(this.onRejectReturnValue)) - : inspect(this.onRejectReturnValue); + const rej = this.onRejectReturnValue + ? trim(String(this.onRejectReturnValue)) + : inspect(this.onRejectReturnValue); const $if = this.ifDep !== null ? this.getDepOptions(this.ifDep).step : null; return `${this.dependencies[0].id}, ${