-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tscircuit:main' into main
- Loading branch information
Showing
5 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { ZodFormattedError } from "zod" | ||
export class InvalidProps extends Error { | ||
constructor( | ||
public readonly componentName: string, | ||
public readonly originalProps: any, | ||
public readonly formattedError: ZodFormattedError<any>, | ||
) { | ||
let message: string | ||
|
||
const propsWithError = Object.keys(formattedError).filter( | ||
(k) => k !== "_errors", | ||
) | ||
|
||
const propMessage = propsWithError | ||
.map((k) => { | ||
if ((formattedError as any)[k]._errors[0]) { | ||
return `${k} (${(formattedError as any)[k]._errors[0]})` | ||
} | ||
return `${k} (${JSON.stringify((formattedError as any)[k])})` | ||
}) | ||
.join(", ") | ||
|
||
if ("name" in originalProps) { | ||
message = `Invalid props for ${componentName} "${originalProps.name}": ${propMessage}` | ||
} else if ( | ||
"footprint" in originalProps && | ||
typeof originalProps.footprint === "string" | ||
) { | ||
message = `Invalid props for ${componentName} (unnamed ${originalProps.footprint} component): ${propMessage}` | ||
} else { | ||
message = `Invalid props for ${componentName} (unnamed): ${propMessage}` | ||
} | ||
|
||
super(message) | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/components/normal-components/resistor-without-resistance.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { test, expect } from "bun:test" | ||
import { InvalidProps } from "lib/errors/InvalidProps" | ||
import { getTestFixture } from "tests/fixtures/get-test-fixture" | ||
import { ZodError } from "zod" | ||
|
||
test("resistor without resistance throws well-formed error", () => { | ||
const { project } = getTestFixture() | ||
|
||
try { | ||
project.add( | ||
<board width="10mm" height="10mm"> | ||
{/* @ts-ignore */} | ||
<resistor name="R1" footprint="0402" /> | ||
</board>, | ||
) | ||
|
||
throw new Error( | ||
"Should not be able to render circuit where resistor has no resistance", | ||
) | ||
} catch (e: unknown) { | ||
expect(e).toBeInstanceOf(InvalidProps) | ||
expect((e as InvalidProps).message).toContain("R1") | ||
expect((e as InvalidProps).message).toContain("resistance") | ||
} | ||
}) |