Skip to content

Commit

Permalink
Change to use a type that isn't string
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran committed Nov 14, 2024
1 parent 53cc147 commit 4affd77
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/clientSideScene/sceneEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/components/ModelingSidebar/ModelingPanes/MemoryPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
})
Expand Down
4 changes: 2 additions & 2 deletions src/lang/queryAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
)
}

Expand Down
14 changes: 8 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 @@ -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
Expand All @@ -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.`)
}
}

Expand All @@ -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
}
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

0 comments on commit 4affd77

Please sign in to comment.