From 73345cb52a72a602feeb6fde9b8c1e452a5b8dc2 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sun, 3 Dec 2023 10:14:00 -0800 Subject: [PATCH] Added types for SqError --- .../src/components/DynamicSquiggleViewer.tsx | 7 ++++++- .../src/components/SquiggleViewer/index.tsx | 7 ++++++- packages/components/src/lib/hooks/useSquiggle.ts | 3 ++- packages/components/src/lib/utility.ts | 11 +++++++++++ .../src/public/SqProject/ProjectItem.ts | 2 +- .../squiggle-lang/src/public/SqProject/index.ts | 8 ++++++-- packages/squiggle-lang/src/public/types.ts | 4 ++-- packages/squiggle-lang/src/reducer/index.ts | 13 +++++++++---- 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/packages/components/src/components/DynamicSquiggleViewer.tsx b/packages/components/src/components/DynamicSquiggleViewer.tsx index 61fc3c5585..d470f34faa 100644 --- a/packages/components/src/components/DynamicSquiggleViewer.tsx +++ b/packages/components/src/components/DynamicSquiggleViewer.tsx @@ -2,7 +2,11 @@ import { forwardRef } from "react"; import { SquiggleViewer } from "../index.js"; import { SquiggleOutput } from "../lib/hooks/useSquiggle.js"; -import { getResultVariables, getResultValue } from "../lib/utility.js"; +import { + getResultVariables, + getResultValue, + getResultError, +} from "../lib/utility.js"; import { CodeEditorHandle } from "./CodeEditor/index.js"; import { PartialPlaygroundSettings } from "./PlaygroundSettings.js"; import { SquiggleViewerHandle } from "./SquiggleViewer/index.js"; @@ -42,6 +46,7 @@ export const DynamicSquiggleViewer = forwardRef( ref={viewerRef} resultVariables={getResultVariables(squiggleOutput)} resultItem={getResultValue(squiggleOutput)} + resultError={getResultError(squiggleOutput)} editor={editor} rootPathOverride={rootPathOverride} /> diff --git a/packages/components/src/components/SquiggleViewer/index.tsx b/packages/components/src/components/SquiggleViewer/index.tsx index 02d0c4efcf..ca37dcface 100644 --- a/packages/components/src/components/SquiggleViewer/index.tsx +++ b/packages/components/src/components/SquiggleViewer/index.tsx @@ -31,6 +31,7 @@ export type SquiggleViewerProps = { /** The output of squiggle's run */ resultVariables: result; resultItem: result | undefined; + resultError: SqError | undefined; editor?: CodeEditorHandle; rootPathOverride?: SqValuePath; } & PartialPlaygroundSettings; @@ -39,9 +40,10 @@ const SquiggleViewerOuter = forwardRef< SquiggleViewerHandle, SquiggleViewerProps >(function SquiggleViewerOuter( - { resultVariables, resultItem, rootPathOverride }, + { resultVariables, resultItem, resultError, rootPathOverride }, ref ) { + console.log("ERROR", resultError); const { focused, dispatch, getCalculator } = useViewerContext(); const unfocus = useUnfocus(); const focus = useFocus(); @@ -137,6 +139,7 @@ const SquiggleViewerOuter = forwardRef< return (
{focusedNavigation} + {resultError && } {body()}
); @@ -147,6 +150,7 @@ const innerComponent = forwardRef( { resultVariables, resultItem, + resultError, editor, rootPathOverride, ...partialPlaygroundSettings @@ -171,6 +175,7 @@ const innerComponent = forwardRef( diff --git a/packages/components/src/lib/hooks/useSquiggle.ts b/packages/components/src/lib/hooks/useSquiggle.ts index 0b202793e2..97be1e34bf 100644 --- a/packages/components/src/lib/hooks/useSquiggle.ts +++ b/packages/components/src/lib/hooks/useSquiggle.ts @@ -7,6 +7,7 @@ import { SqDict, SqValue, result, + SqRuntimeError, } from "@quri/squiggle-lang"; import { WINDOW_VARIABLE_NAME } from "../constants.js"; @@ -38,7 +39,7 @@ export type SquiggleOutput = { exports: SqDict; result: SqValue; bindings: SqDict; - error: any; + error: SqRuntimeError | undefined; }, SqError >; diff --git a/packages/components/src/lib/utility.ts b/packages/components/src/lib/utility.ts index ef6046ca88..74d2efeb01 100644 --- a/packages/components/src/lib/utility.ts +++ b/packages/components/src/lib/utility.ts @@ -4,6 +4,7 @@ import { SqDictValue, result, resultMap, + SqRuntimeError, } from "@quri/squiggle-lang"; import { SquiggleOutput } from "./hooks/useSquiggle.js"; @@ -59,6 +60,16 @@ export function getResultValue({ } } +export function getResultError({ + output, +}: SquiggleOutput): SqRuntimeError | undefined { + if (output.ok) { + return output.value.error; + } else { + return undefined; + } +} + export function getErrors(result: SquiggleOutput["output"]) { if (!result.ok) { return [result.value]; diff --git a/packages/squiggle-lang/src/public/SqProject/ProjectItem.ts b/packages/squiggle-lang/src/public/SqProject/ProjectItem.ts index 5a4a822962..430bcc19c3 100644 --- a/packages/squiggle-lang/src/public/SqProject/ProjectItem.ts +++ b/packages/squiggle-lang/src/public/SqProject/ProjectItem.ts @@ -17,7 +17,7 @@ export type RunOutput = { result: Value; bindings: Bindings; exports: Bindings; - error: IRuntimeError | undefined; + error: SqError | undefined; }; export type Import = diff --git a/packages/squiggle-lang/src/public/SqProject/index.ts b/packages/squiggle-lang/src/public/SqProject/index.ts index b0e541afde..dae83b51b5 100644 --- a/packages/squiggle-lang/src/public/SqProject/index.ts +++ b/packages/squiggle-lang/src/public/SqProject/index.ts @@ -7,7 +7,7 @@ import { ImmutableMap } from "../../utility/immutableMap.js"; import * as Result from "../../utility/result.js"; import { Value, vDict } from "../../value/index.js"; -import { SqError, SqOtherError } from "../SqError.js"; +import { SqError, SqOtherError, SqRuntimeError } from "../SqError.js"; import { SqDict } from "../SqValue/SqDict.js"; import { SqValue, wrapValue } from "../SqValue/index.js"; import { SqValueContext } from "../SqValueContext.js"; @@ -16,6 +16,7 @@ import { SqValuePath } from "../SqValuePath.js"; import { SqLinker } from "../SqLinker.js"; import { SqOutputResult } from "../types.js"; import { Import, ProjectItem, RunOutput } from "./ProjectItem.js"; +import { IRuntimeError } from "../../errors/IError.js"; function getNeedToRunError() { return new SqOtherError("Need to run"); @@ -267,11 +268,14 @@ export class SqProject { ) ); + const error = internalOutputR.value.error; return Result.Ok({ result, bindings, exports, - error: internalOutputR.value.error, + error: error + ? new SqRuntimeError(IRuntimeError.fromException(error)) + : undefined, }); } diff --git a/packages/squiggle-lang/src/public/types.ts b/packages/squiggle-lang/src/public/types.ts index 4f44bf3993..5516ab354e 100644 --- a/packages/squiggle-lang/src/public/types.ts +++ b/packages/squiggle-lang/src/public/types.ts @@ -1,5 +1,5 @@ import { result } from "../utility/result.js"; -import { SqError } from "./SqError.js"; +import { SqError, SqRuntimeError } from "./SqError.js"; import { SqValue } from "./SqValue/index.js"; import { SqDict } from "./SqValue/SqDict.js"; @@ -7,7 +7,7 @@ export type SqOutput = { result: SqValue; bindings: SqDict; exports: SqDict; - error: any; + error: SqRuntimeError | undefined; }; export type SqOutputResult = result; diff --git a/packages/squiggle-lang/src/reducer/index.ts b/packages/squiggle-lang/src/reducer/index.ts index 185c85d6e1..fd8413fb6f 100644 --- a/packages/squiggle-lang/src/reducer/index.ts +++ b/packages/squiggle-lang/src/reducer/index.ts @@ -13,6 +13,7 @@ import { } from "../errors/messages.js"; import { compileAst } from "../expression/compile.js"; import { Expression } from "../expression/index.js"; +import { SqError, SqRuntimeError } from "../index.js"; import { getStdLib } from "../library/index.js"; import { ImmutableMap } from "../utility/immutableMap.js"; import * as Result from "../utility/result.js"; @@ -33,13 +34,13 @@ import { UserDefinedLambdaParameter, UserDefinedLambda } from "./lambda.js"; export type ReducerFn = ( expression: Expression, context: Context.ReducerContext -) => [Value, Context.ReducerContext, any | undefined]; +) => [Value, Context.ReducerContext, SqError | undefined]; type SubReducerFn = ( expressionValue: Extract["value"], context: Context.ReducerContext, ast: ASTNode -) => [Value, Context.ReducerContext, any | undefined]; +) => [Value, Context.ReducerContext, SqError | undefined]; function throwFrom( error: ErrorMessage, @@ -118,8 +119,12 @@ const evaluateProgram: SubReducerFn<"Program"> = (expressionValue, context) => { statement, currentContext ); - } catch (e) { - return [currentValue, currentContext, e]; + } catch (e: unknown) { + return [ + currentValue, + currentContext, + new SqRuntimeError(IRuntimeError.fromException(e)), + ]; } } return [currentValue, currentContext, undefined];