Skip to content

Commit

Permalink
Overhaul printing of flagged values
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Apr 30, 2024
1 parent 5226285 commit 3957edb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
21 changes: 21 additions & 0 deletions grafast/grafast/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inspect } from "./inspect.js";
import type { ExecutionEntryFlags } from "./interfaces.js";
import {
$$safeError,
Expand All @@ -18,18 +19,38 @@ export interface FlaggedValue<TValue = any> {
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<T>(
flags: ExecutionEntryFlags,
value: any,
planId: null | number,
): FlaggedValue<T> {
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,
};
}

Expand Down
11 changes: 4 additions & 7 deletions grafast/grafast/src/steps/__flag.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -138,12 +138,9 @@ export class __FlagStep<TData> extends ExecutableStep<TData> {
}
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}, ${
Expand Down

0 comments on commit 3957edb

Please sign in to comment.