From 9362392eeb89316cad2e36fd7b006f0cdcb95506 Mon Sep 17 00:00:00 2001 From: Vahap Ogut Date: Mon, 22 Jun 2026 15:10:25 +0300 Subject: [PATCH 1/2] fix: clarify footprint function errors --- src/footprinter.ts | 31 +++++++++++++++++-------------- tests/fp-string-error.test.ts | 16 ++++++++++++++-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/footprinter.ts b/src/footprinter.ts index fa51a3b4..a13b4df6 100644 --- a/src/footprinter.ts +++ b/src/footprinter.ts @@ -277,6 +277,20 @@ const normalizeDefinition = (def: string): string => { .replace(/^jst_(ph|sh|zh)_(\d+)(?=_|$)/i, "jst$2_$1") } +const createInvalidFootprintFunctionError = (target: any): Error => { + if (target.fn) { + return new Error( + `Function not found for footprinter "${target.fn}". Specify a valid function like .dip, .lr, .p etc.${ + target.string ? ` From string "${target.string}".` : "" + }`, + ) + } + + return new Error( + "No footprint function selected. Specify a valid function like .dip, .lr, .p etc.", + ) +} + export const string = (def: string): Footprinter => { let fp = footprinter() const normalizedDef = normalizeDefinition(def) @@ -363,27 +377,16 @@ export const footprinter = (): Footprinter & { } if (!FOOTPRINT_FN[target.fn]) { - throw new Error( - `Invalid footprint function, got "${target.fn}"${ - target.string ? `, from string "${target.string}"` : "" - }`, - ) + throw createInvalidFootprintFunctionError(target) } return () => { - // TODO improve error - throw new Error( - `No function found for footprinter, make sure to specify .dip, .lr, .p, etc. Got "${prop}"`, - ) + throw createInvalidFootprintFunctionError(target) } } if (prop === "json") { if (!FOOTPRINT_FN[target.fn]) { - throw new Error( - `Invalid footprint function, got "${target.fn}"${ - target.string ? `, from string "${target.string}"` : "" - }`, - ) + throw createInvalidFootprintFunctionError(target) } return () => FOOTPRINT_FN[target.fn](target).parameters } diff --git a/tests/fp-string-error.test.ts b/tests/fp-string-error.test.ts index 1e6c4227..fb4bb856 100644 --- a/tests/fp-string-error.test.ts +++ b/tests/fp-string-error.test.ts @@ -1,8 +1,20 @@ -import { test, expect } from "bun:test" +import { expect, test } from "bun:test" import { fp } from "src/footprinter" test("fp.string error", () => { expect(() => fp.string("nonexistentfn4_p3").circuitJson()).toThrow( - 'Invalid footprint function, got "nonexistentfn", from string "nonexistentfn4_p3"', + 'Function not found for footprinter "nonexistentfn". Specify a valid function like .dip, .lr, .p etc. From string "nonexistentfn4_p3".', + ) +}) + +test("fluent invalid function error names the missing function", () => { + expect(() => fp().invalid().circuitJson()).toThrow( + 'Function not found for footprinter "invalid". Specify a valid function like .dip, .lr, .p etc.', + ) +}) + +test("empty footprinter error asks for a valid function", () => { + expect(() => fp().circuitJson()).toThrow( + "No footprint function selected. Specify a valid function like .dip, .lr, .p etc.", ) }) From 843cf0ee3922cb58fdf4e63a36df4c4148037994 Mon Sep 17 00:00:00 2001 From: Vahap Ogut Date: Mon, 22 Jun 2026 15:28:22 +0300 Subject: [PATCH 2/2] test: align invalid footprint error expectation --- tests/vssop.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/vssop.test.ts b/tests/vssop.test.ts index 0b12cc27..acfd5f93 100644 --- a/tests/vssop.test.ts +++ b/tests/vssop.test.ts @@ -1,4 +1,4 @@ -import { test, expect } from "bun:test" +import { expect, test } from "bun:test" import { convertCircuitJsonToPcbSvg } from "circuit-to-svg" import { fp } from "../src/footprinter" @@ -123,7 +123,7 @@ test("invalid_vssop6", () => { } catch (error) { const e = error as Error expect(e).toBeInstanceOf(Error) - expect(e.message).toContain("Invalid footprint function") + expect(e.message).toContain('Function not found for footprinter "invalid"') } })