Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to not have unneeded console errors #4482

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/clientSideScene/sceneEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
VariableDeclaration,
VariableDeclarator,
sketchFromKclValue,
sketchFromKclValueOptional,
} from 'lang/wasm'
import {
engineCommandManager,
Expand Down Expand Up @@ -91,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'
Expand Down Expand Up @@ -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 (!(sk instanceof Reason)) {
sketch = sk
} else if ((maybeSketch as Solid).sketch) {
sketch = (maybeSketch as Solid).sketch
}
Expand Down
10 changes: 5 additions & 5 deletions src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 { Reason, trap } from 'lib/trap'
import Tooltip from 'components/Tooltip'
import { useModelingContext } from 'hooks/useModelingContext'

Expand Down Expand Up @@ -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 (!(sk instanceof Reason)) {
processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => {
Comment on lines -101 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can assure me this is actually not an error, I'll approve this to add to our toolbox.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? The return type of sketchFromKclValueOptional() cannot return an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this component is to display everything in memory in the UI. The only reason it checks for a sketch is so that it can be displayed differently from other types of values.

return rest
})
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/lang/queryAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ProgramMemory,
ReturnStatement,
sketchFromKclValue,
sketchFromKclValueOptional,
SourceRange,
SyntaxType,
VariableDeclaration,
Expand All @@ -27,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'

Expand Down Expand Up @@ -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' ||
!(sketchFromKclValueOptional(varValue, varName) instanceof Reason)
)
}

Expand Down
23 changes: 17 additions & 6 deletions src/lang/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 | 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
Expand All @@ -369,13 +369,24 @@ export function sketchFromKclValue(
}
const actualType = obj?.value?.type ?? obj?.type
if (actualType) {
console.log(obj)
return new Error(
return new Reason(
`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 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 (
Expand Down
17 changes: 17 additions & 0 deletions src/lib/trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ import toast from 'react-hot-toast'

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.
*/
Expand Down
Loading