From 53cc14774d75431b4caf28a1eec15eaaab66f054 Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Wed, 13 Nov 2024 12:33:52 -0500 Subject: [PATCH 1/2] Fix to not have unneeded console errors --- src/clientSideScene/sceneEntities.ts | 12 ++++++---- .../ModelingPanes/MemoryPane.tsx | 10 ++++---- src/lang/queryAst.ts | 4 +++- src/lang/wasm.ts | 23 +++++++++++++------ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/clientSideScene/sceneEntities.ts b/src/clientSideScene/sceneEntities.ts index 77d4f9e9de..bf29bae2b5 100644 --- a/src/clientSideScene/sceneEntities.ts +++ b/src/clientSideScene/sceneEntities.ts @@ -47,6 +47,7 @@ import { VariableDeclaration, VariableDeclarator, sketchFromKclValue, + sketchFromKclValueOptional, } from 'lang/wasm' import { engineCommandManager, @@ -1511,10 +1512,13 @@ export class SceneEntities { this.sceneProgramMemory = programMemory const maybeSketch = programMemory.get(variableDeclarationName) - let sketch = undefined - const sg = sketchFromKclValue(maybeSketch, variableDeclarationName) - if (!err(sg)) { - sketch = sg + let sketch: Sketch | undefined + const sk = sketchFromKclValueOptional( + maybeSketch, + variableDeclarationName + ) + if (typeof sk !== 'string') { + sketch = sk } else if ((maybeSketch as Solid).sketch) { sketch = (maybeSketch as Solid).sketch } diff --git a/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx b/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx index 6d737595a7..d3597b20c5 100644 --- a/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx +++ b/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx @@ -5,12 +5,12 @@ import { ProgramMemory, Path, ExtrudeSurface, - sketchFromKclValue, + sketchFromKclValueOptional, } from 'lang/wasm' import { useKclContext } from 'lang/KclProvider' import { useResolvedTheme } from 'hooks/useResolvedTheme' import { ActionButton } from 'components/ActionButton' -import { err, trap } from 'lib/trap' +import { trap } from 'lib/trap' import Tooltip from 'components/Tooltip' import { useModelingContext } from 'hooks/useModelingContext' @@ -93,13 +93,13 @@ export const processMemory = (programMemory: ProgramMemory) => { // @ts-ignore val.type !== 'Function' ) { - const sg = sketchFromKclValue(val, key) + const sk = sketchFromKclValueOptional(val, key) if (val.type === 'Solid') { processedMemory[key] = val.value.map(({ ...rest }: ExtrudeSurface) => { return rest }) - } else if (!err(sg)) { - processedMemory[key] = sg.paths.map(({ __geoMeta, ...rest }: Path) => { + } else if (typeof sk !== 'string') { + processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => { return rest }) } else { diff --git a/src/lang/queryAst.ts b/src/lang/queryAst.ts index 1c0f014232..ca797a4e77 100644 --- a/src/lang/queryAst.ts +++ b/src/lang/queryAst.ts @@ -14,6 +14,7 @@ import { ProgramMemory, ReturnStatement, sketchFromKclValue, + sketchFromKclValueOptional, SourceRange, SyntaxType, VariableDeclaration, @@ -790,7 +791,8 @@ export function hasExtrudeSketch({ const varName = varDec.declarations[0].id.name const varValue = programMemory?.get(varName) return ( - varValue?.type === 'Solid' || !err(sketchFromKclValue(varValue, varName)) + varValue?.type === 'Solid' || + typeof sketchFromKclValueOptional(varValue, varName) !== 'string' ) } diff --git a/src/lang/wasm.ts b/src/lang/wasm.ts index 80a49b4be9..28ab05107a 100644 --- a/src/lang/wasm.ts +++ b/src/lang/wasm.ts @@ -357,10 +357,10 @@ export class ProgramMemory { } // TODO: In the future, make the parameter be a KclValue. -export function sketchFromKclValue( +export function sketchFromKclValueOptional( obj: any, varName: string | null -): Sketch | Error { +): Sketch | string { if (obj?.value?.type === 'Sketch') return obj.value if (obj?.value?.type === 'Solid') return obj.value.sketch if (obj?.type === 'Solid') return obj.sketch @@ -369,13 +369,22 @@ export function sketchFromKclValue( } const actualType = obj?.value?.type ?? obj?.type if (actualType) { - console.log(obj) - return new Error( - `Expected ${varName} to be a sketch or solid, but it was ${actualType} instead.` - ) + return `Expected ${varName} to be a sketch or solid, but it was ${actualType} instead.` } else { - return new Error(`Expected ${varName} to be a sketch, but it wasn't.`) + return `Expected ${varName} to be a sketch, but it wasn't.` + } +} + +// TODO: In the future, make the parameter be a KclValue. +export function sketchFromKclValue( + obj: any, + varName: string | null +): Sketch | Error { + const result = sketchFromKclValueOptional(obj, varName) + if (typeof result === 'string') { + return new Error(result) } + return result } export const executor = async ( From 4affd77f1d798616409181d4ad7ec425f86a60e5 Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Wed, 13 Nov 2024 12:53:36 -0500 Subject: [PATCH 2/2] Change to use a type that isn't string --- src/clientSideScene/sceneEntities.ts | 4 ++-- .../ModelingPanes/MemoryPane.tsx | 4 ++-- src/lang/queryAst.ts | 4 ++-- src/lang/wasm.ts | 14 ++++++++------ src/lib/trap.ts | 17 +++++++++++++++++ 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/clientSideScene/sceneEntities.ts b/src/clientSideScene/sceneEntities.ts index bf29bae2b5..c9f1f35192 100644 --- a/src/clientSideScene/sceneEntities.ts +++ b/src/clientSideScene/sceneEntities.ts @@ -92,7 +92,7 @@ import { updateRectangleSketch, } from 'lib/rectangleTool' import { getThemeColorForThreeJs, Themes } from 'lib/theme' -import { err, reportRejection, trap } from 'lib/trap' +import { err, Reason, reportRejection, trap } from 'lib/trap' import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer' import { Point3d } from 'wasm-lib/kcl/bindings/Point3d' import { SegmentInputs } from 'lang/std/stdTypes' @@ -1517,7 +1517,7 @@ export class SceneEntities { maybeSketch, variableDeclarationName ) - if (typeof sk !== 'string') { + if (!(sk instanceof Reason)) { sketch = sk } else if ((maybeSketch as Solid).sketch) { sketch = (maybeSketch as Solid).sketch diff --git a/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx b/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx index d3597b20c5..9e7ba83a83 100644 --- a/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx +++ b/src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx @@ -10,7 +10,7 @@ import { import { useKclContext } from 'lang/KclProvider' import { useResolvedTheme } from 'hooks/useResolvedTheme' import { ActionButton } from 'components/ActionButton' -import { trap } from 'lib/trap' +import { Reason, trap } from 'lib/trap' import Tooltip from 'components/Tooltip' import { useModelingContext } from 'hooks/useModelingContext' @@ -98,7 +98,7 @@ export const processMemory = (programMemory: ProgramMemory) => { processedMemory[key] = val.value.map(({ ...rest }: ExtrudeSurface) => { return rest }) - } else if (typeof sk !== 'string') { + } else if (!(sk instanceof Reason)) { processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => { return rest }) diff --git a/src/lang/queryAst.ts b/src/lang/queryAst.ts index ca797a4e77..61f8f892cb 100644 --- a/src/lang/queryAst.ts +++ b/src/lang/queryAst.ts @@ -28,7 +28,7 @@ import { getConstraintLevelFromSourceRange, getConstraintType, } from './std/sketchcombos' -import { err } from 'lib/trap' +import { err, Reason } from 'lib/trap' import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement' import { Node } from 'wasm-lib/kcl/bindings/Node' @@ -792,7 +792,7 @@ export function hasExtrudeSketch({ const varValue = programMemory?.get(varName) return ( varValue?.type === 'Solid' || - typeof sketchFromKclValueOptional(varValue, varName) !== 'string' + !(sketchFromKclValueOptional(varValue, varName) instanceof Reason) ) } diff --git a/src/lang/wasm.ts b/src/lang/wasm.ts index 28ab05107a..5c78a76106 100644 --- a/src/lang/wasm.ts +++ b/src/lang/wasm.ts @@ -32,7 +32,7 @@ import { CoreDumpManager } from 'lib/coredump' import openWindow from 'lib/openWindow' import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes' import { TEST } from 'env' -import { err } from 'lib/trap' +import { err, Reason } from 'lib/trap' import { Configuration } from 'wasm-lib/kcl/bindings/Configuration' import { DeepPartial } from 'lib/types' import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration' @@ -360,7 +360,7 @@ export class ProgramMemory { export function sketchFromKclValueOptional( obj: any, varName: string | null -): Sketch | string { +): Sketch | Reason { if (obj?.value?.type === 'Sketch') return obj.value if (obj?.value?.type === 'Solid') return obj.value.sketch if (obj?.type === 'Solid') return obj.sketch @@ -369,9 +369,11 @@ export function sketchFromKclValueOptional( } const actualType = obj?.value?.type ?? obj?.type if (actualType) { - return `Expected ${varName} to be a sketch or solid, but it was ${actualType} instead.` + return new Reason( + `Expected ${varName} to be a sketch or solid, but it was ${actualType} instead.` + ) } else { - return `Expected ${varName} to be a sketch, but it wasn't.` + return new Reason(`Expected ${varName} to be a sketch, but it wasn't.`) } } @@ -381,8 +383,8 @@ export function sketchFromKclValue( varName: string | null ): Sketch | Error { const result = sketchFromKclValueOptional(obj, varName) - if (typeof result === 'string') { - return new Error(result) + if (result instanceof Reason) { + return result.toError() } return result } diff --git a/src/lib/trap.ts b/src/lib/trap.ts index 686571d79b..ab4551a42a 100644 --- a/src/lib/trap.ts +++ b/src/lib/trap.ts @@ -2,6 +2,23 @@ import toast from 'react-hot-toast' type ExcludeErr = Exclude +/** + * Similar to Error, but more lightweight, without the stack trace. It can also + * be used to represent a reason for not being able to provide an alternative, + * which isn't necessarily an error. + */ +export class Reason { + message: string + + constructor(message: string) { + this.message = message + } + + toError() { + return new Error(this.message) + } +} + /** * This is intentionally *not* exported due to misuse. We'd like to add a lint. */