Fix to not have unneeded console errors (#4482)

* Fix to not have unneeded console errors

* Change to use a type that isn't string
This commit is contained in:
Jonathan Tran
2024-11-19 17:34:54 -05:00
committed by GitHub
parent 54b5774f9e
commit 1a9926be8a
5 changed files with 52 additions and 18 deletions

View File

@ -47,6 +47,7 @@ import {
VariableDeclaration, VariableDeclaration,
VariableDeclarator, VariableDeclarator,
sketchFromKclValue, sketchFromKclValue,
sketchFromKclValueOptional,
} from 'lang/wasm' } from 'lang/wasm'
import { import {
engineCommandManager, engineCommandManager,
@ -92,7 +93,7 @@ import {
updateCenterRectangleSketch, updateCenterRectangleSketch,
} from 'lib/rectangleTool' } from 'lib/rectangleTool'
import { getThemeColorForThreeJs, Themes } from 'lib/theme' 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 { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'
import { Point3d } from 'wasm-lib/kcl/bindings/Point3d' import { Point3d } from 'wasm-lib/kcl/bindings/Point3d'
import { SegmentInputs } from 'lang/std/stdTypes' import { SegmentInputs } from 'lang/std/stdTypes'
@ -1697,10 +1698,13 @@ export class SceneEntities {
this.sceneProgramMemory = programMemory this.sceneProgramMemory = programMemory
const maybeSketch = programMemory.get(variableDeclarationName) const maybeSketch = programMemory.get(variableDeclarationName)
let sketch = undefined let sketch: Sketch | undefined
const sg = sketchFromKclValue(maybeSketch, variableDeclarationName) const sk = sketchFromKclValueOptional(
if (!err(sg)) { maybeSketch,
sketch = sg variableDeclarationName
)
if (!(sk instanceof Reason)) {
sketch = sk
} else if ((maybeSketch as Solid).sketch) { } else if ((maybeSketch as Solid).sketch) {
sketch = (maybeSketch as Solid).sketch sketch = (maybeSketch as Solid).sketch
} }

View File

@ -5,12 +5,12 @@ import {
ProgramMemory, ProgramMemory,
Path, Path,
ExtrudeSurface, ExtrudeSurface,
sketchFromKclValue, sketchFromKclValueOptional,
} from 'lang/wasm' } from 'lang/wasm'
import { useKclContext } from 'lang/KclProvider' import { useKclContext } from 'lang/KclProvider'
import { useResolvedTheme } from 'hooks/useResolvedTheme' import { useResolvedTheme } from 'hooks/useResolvedTheme'
import { ActionButton } from 'components/ActionButton' import { ActionButton } from 'components/ActionButton'
import { err, trap } from 'lib/trap' import { Reason, trap } from 'lib/trap'
import Tooltip from 'components/Tooltip' import Tooltip from 'components/Tooltip'
import { useModelingContext } from 'hooks/useModelingContext' import { useModelingContext } from 'hooks/useModelingContext'
@ -93,13 +93,13 @@ export const processMemory = (programMemory: ProgramMemory) => {
// @ts-ignore // @ts-ignore
val.type !== 'Function' val.type !== 'Function'
) { ) {
const sg = sketchFromKclValue(val, key) const sk = sketchFromKclValueOptional(val, key)
if (val.type === 'Solid') { if (val.type === 'Solid') {
processedMemory[key] = val.value.map(({ ...rest }: ExtrudeSurface) => { processedMemory[key] = val.value.map(({ ...rest }: ExtrudeSurface) => {
return rest return rest
}) })
} else if (!err(sg)) { } else if (!(sk instanceof Reason)) {
processedMemory[key] = sg.paths.map(({ __geoMeta, ...rest }: Path) => { processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => {
return rest return rest
}) })
} else { } else {

View File

@ -14,6 +14,7 @@ import {
ProgramMemory, ProgramMemory,
ReturnStatement, ReturnStatement,
sketchFromKclValue, sketchFromKclValue,
sketchFromKclValueOptional,
SourceRange, SourceRange,
SyntaxType, SyntaxType,
VariableDeclaration, VariableDeclaration,
@ -27,7 +28,7 @@ import {
getConstraintLevelFromSourceRange, getConstraintLevelFromSourceRange,
getConstraintType, getConstraintType,
} from './std/sketchcombos' } from './std/sketchcombos'
import { err } from 'lib/trap' import { err, Reason } from 'lib/trap'
import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement' import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement'
import { Node } from 'wasm-lib/kcl/bindings/Node' import { Node } from 'wasm-lib/kcl/bindings/Node'
@ -846,7 +847,8 @@ export function hasExtrudeSketch({
const varName = varDec.declarations[0].id.name const varName = varDec.declarations[0].id.name
const varValue = programMemory?.get(varName) const varValue = programMemory?.get(varName)
return ( return (
varValue?.type === 'Solid' || !err(sketchFromKclValue(varValue, varName)) varValue?.type === 'Solid' ||
!(sketchFromKclValueOptional(varValue, varName) instanceof Reason)
) )
} }

View File

@ -32,7 +32,7 @@ import { CoreDumpManager } from 'lib/coredump'
import openWindow from 'lib/openWindow' import openWindow from 'lib/openWindow'
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes' import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
import { TEST } from 'env' import { TEST } from 'env'
import { err } from 'lib/trap' import { err, Reason } from 'lib/trap'
import { Configuration } from 'wasm-lib/kcl/bindings/Configuration' import { Configuration } from 'wasm-lib/kcl/bindings/Configuration'
import { DeepPartial } from 'lib/types' import { DeepPartial } from 'lib/types'
import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration' import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration'
@ -365,10 +365,10 @@ export class ProgramMemory {
} }
// TODO: In the future, make the parameter be a KclValue. // TODO: In the future, make the parameter be a KclValue.
export function sketchFromKclValue( export function sketchFromKclValueOptional(
obj: any, obj: any,
varName: string | null varName: string | null
): Sketch | Error { ): Sketch | Reason {
if (obj?.value?.type === 'Sketch') return obj.value if (obj?.value?.type === 'Sketch') return obj.value
if (obj?.value?.type === 'Solid') return obj.value.sketch if (obj?.value?.type === 'Solid') return obj.value.sketch
if (obj?.type === 'Solid') return obj.sketch if (obj?.type === 'Solid') return obj.sketch
@ -377,15 +377,26 @@ export function sketchFromKclValue(
} }
const actualType = obj?.value?.type ?? obj?.type const actualType = obj?.value?.type ?? obj?.type
if (actualType) { if (actualType) {
console.log(obj) return new Reason(
return new Error(
`Expected ${varName} to be a sketch or solid, but it was ${actualType} instead.` `Expected ${varName} to be a sketch or solid, but it was ${actualType} instead.`
) )
} else { } else {
return new Error(`Expected ${varName} to be a sketch, but it wasn't.`) return new Reason(`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 (result instanceof Reason) {
return result.toError()
}
return result
}
export const executor = async ( export const executor = async (
node: Node<Program>, node: Node<Program>,
programMemory: ProgramMemory | Error = ProgramMemory.empty(), programMemory: ProgramMemory | Error = ProgramMemory.empty(),

View File

@ -2,6 +2,23 @@ import toast from 'react-hot-toast'
type ExcludeErr<T> = Exclude<T, Error> type ExcludeErr<T> = Exclude<T, Error>
/**
* 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. * This is intentionally *not* exported due to misuse. We'd like to add a lint.
*/ */