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

patch: improve error message #609

Merged
merged 3 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions lib/components/base-components/PrimitiveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ export abstract class PrimitiveComponent<
}

add(component: PrimitiveComponent) {
if (!component.onAddToParent) {
throw new Error(
"Invalid JSX Element: Expected a React component but received a non-component value. Ensure you are using a valid component (function or class) and that it's properly imported.",
Copy link
Contributor

Choose a reason for hiding this comment

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

can you attempt to stringify it to make the error message even better.

Also no need for the latter half

Suggested change
"Invalid JSX Element: Expected a React component but received a non-component value. Ensure you are using a valid component (function or class) and that it's properly imported.",
"Invalid JSX Element: Expected a React component but received "...".

)
}
component.onAddToParent(this)
component.parent = this
this.children.push(component)
Expand Down
14 changes: 14 additions & 0 deletions tests/components/primitive-components/invalid-jsx.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from "bun:test"
import { Circuit } from "lib"

test("snippet-import1-contribution-board", async () => {
const circuit = new Circuit()

expect(() => {
circuit.add(
<board width="10mm" height="10mm">
{1}
</board>,
)
}).toThrowError("Invalid JSX Element")
})
Copy link
Contributor

Choose a reason for hiding this comment

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

make sure to test with new ClassName() because i think JSON.stringify could error

Copy link
Contributor

Choose a reason for hiding this comment

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

or you could put in a function or something