Skip to content

fix: warn on malformed items JSON instead of silently swallowing it - #7

Open
rajarshi1 wants to merge 1 commit into
kirthi-b:masterfrom
rajarshi1:warn-on-malformed-items-json
Open

fix: warn on malformed items JSON instead of silently swallowing it#7
rajarshi1 wants to merge 1 commit into
kirthi-b:masterfrom
rajarshi1:warn-on-malformed-items-json

Conversation

@rajarshi1

Copy link
Copy Markdown

Warn on malformed items JSON

Fixes #4

Problem

<folder-gallery> accepts a declarative items attribute as a JSON string. In connectedCallback, parsing it was wrapped in a try/catch with an empty catch block:

try { this._items = JSON.parse(this.getAttribute('items')); } catch (_) { /* ignore malformed */ }

If the JSON is malformed (a typo, a trailing comma, etc.), the element just silently renders an empty gallery with zero indication of why — making it confusing to debug.

Fix

Log a console.warn with the parse error instead of swallowing it:

try { this._items = JSON.parse(this.getAttribute('items')); } catch (err) { console.warn('<folder-gallery> malformed items attribute:', err.message); }

Added a test in tests/element.test.js asserting the warning fires exactly once with a message matching /malformed items attribute/, using vi.spyOn(console, 'warn').

Testing

  • npm test — 41/41 passing (up from 40, new test included)
  • npm run check — clean
  • npm run build — clean (ESM + CJS)

Files changed

  • src/folder-gallery-element.js
  • tests/element.test.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Element: warn on malformed items JSON

1 participant