Skip to content

Commit

Permalink
Added types for SqError
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Dec 3, 2023
1 parent accd314 commit 73345cb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 12 deletions.
7 changes: 6 additions & 1 deletion packages/components/src/components/DynamicSquiggleViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -42,6 +46,7 @@ export const DynamicSquiggleViewer = forwardRef<SquiggleViewerHandle, Props>(
ref={viewerRef}
resultVariables={getResultVariables(squiggleOutput)}
resultItem={getResultValue(squiggleOutput)}
resultError={getResultError(squiggleOutput)}
editor={editor}
rootPathOverride={rootPathOverride}
/>
Expand Down
7 changes: 6 additions & 1 deletion packages/components/src/components/SquiggleViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type SquiggleViewerProps = {
/** The output of squiggle's run */
resultVariables: result<SqDictValue, SqError>;
resultItem: result<SqValue, SqError> | undefined;
resultError: SqError | undefined;
editor?: CodeEditorHandle;
rootPathOverride?: SqValuePath;
} & PartialPlaygroundSettings;
Expand All @@ -39,9 +40,10 @@ const SquiggleViewerOuter = forwardRef<
SquiggleViewerHandle,
SquiggleViewerProps
>(function SquiggleViewerOuter(
{ resultVariables, resultItem, rootPathOverride },
{ resultVariables, resultItem, resultError, rootPathOverride },
ref
) {
console.log("ERROR", resultError);

Check warning on line 46 in packages/components/src/components/SquiggleViewer/index.tsx

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected console statement
const { focused, dispatch, getCalculator } = useViewerContext();
const unfocus = useUnfocus();
const focus = useFocus();
Expand Down Expand Up @@ -137,6 +139,7 @@ const SquiggleViewerOuter = forwardRef<
return (
<div>
{focusedNavigation}
{resultError && <SquiggleErrorAlert error={resultError} />}
{body()}
</div>
);
Expand All @@ -147,6 +150,7 @@ const innerComponent = forwardRef<SquiggleViewerHandle, SquiggleViewerProps>(
{
resultVariables,
resultItem,
resultError,
editor,
rootPathOverride,
...partialPlaygroundSettings
Expand All @@ -171,6 +175,7 @@ const innerComponent = forwardRef<SquiggleViewerHandle, SquiggleViewerProps>(
<SquiggleViewerOuter
resultVariables={resultVariables}
resultItem={resultItem}
resultError={resultError}
rootPathOverride={rootPathOverride}
ref={ref}
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/lib/hooks/useSquiggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SqDict,
SqValue,
result,
SqRuntimeError,
} from "@quri/squiggle-lang";
import { WINDOW_VARIABLE_NAME } from "../constants.js";

Expand Down Expand Up @@ -38,7 +39,7 @@ export type SquiggleOutput = {
exports: SqDict;
result: SqValue;
bindings: SqDict;
error: any;
error: SqRuntimeError | undefined;
},
SqError
>;
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/lib/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SqDictValue,
result,
resultMap,
SqRuntimeError,
} from "@quri/squiggle-lang";

import { SquiggleOutput } from "./hooks/useSquiggle.js";
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/public/SqProject/ProjectItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type RunOutput = {
result: Value;
bindings: Bindings;
exports: Bindings;
error: IRuntimeError | undefined;
error: SqError | undefined;
};

export type Import =
Expand Down
8 changes: 6 additions & 2 deletions packages/squiggle-lang/src/public/SqProject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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");
Expand Down Expand Up @@ -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,
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/public/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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";

export type SqOutput = {
result: SqValue;
bindings: SqDict;
exports: SqDict;
error: any;
error: SqRuntimeError | undefined;
};

export type SqOutputResult = result<SqOutput, SqError>;
13 changes: 9 additions & 4 deletions packages/squiggle-lang/src/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<T extends Expression["type"] = Expression["type"]> = (
expressionValue: Extract<Expression, { type: T }>["value"],
context: Context.ReducerContext,
ast: ASTNode
) => [Value, Context.ReducerContext, any | undefined];
) => [Value, Context.ReducerContext, SqError | undefined];

function throwFrom(
error: ErrorMessage,
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 73345cb

Please sign in to comment.