-
-
Notifications
You must be signed in to change notification settings - Fork 3
Include page path in PageData.vars error messages #242
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
Open
bcomnes
wants to merge
5
commits into
master
Choose a base branch
from
fix/vars-getter-error-messages
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−8
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
866a2eb
wrap PageData.vars getter to include page path in error messages
bcomnes fb4d4b0
Include page path in uninitialized vars error message
bcomnes 459b054
Add regression tests for PageData.vars error messages
bcomnes 4366bc0
Fix catch path to use optional chaining; add catch block regression test
bcomnes 20e00fd
fix: add name to fakeLayout and type children param to pass tsc
bcomnes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,72 @@ | ||
| import { test } from 'node:test' | ||
| import assert from 'node:assert' | ||
| import { mkdtemp, writeFile, rm } from 'node:fs/promises' | ||
| import { join } from 'node:path' | ||
| import { tmpdir } from 'node:os' | ||
| import { PageData } from './page-data.js' | ||
|
|
||
| const fakeLayout = { | ||
| name: 'default', | ||
| render: async (/** @type {any} */ { children }) => String(children), | ||
| layoutStylePath: null, | ||
| layoutClientPath: null, | ||
| } | ||
|
|
||
| test.describe('PageData.vars catch block wrapping', () => { | ||
| test('wraps error from spread merge with page path and preserves cause', async () => { | ||
| const dir = await mkdtemp(join(tmpdir(), 'domstack-pagedata-test-')) | ||
| const mdFile = join(dir, 'test.md') | ||
|
|
||
| try { | ||
| await writeFile(mdFile, '# Test\n\nContent.') | ||
|
|
||
| const pd = new PageData({ | ||
| pageInfo: /** @type {any} */ ({ | ||
| path: 'blog/post', | ||
| outputName: 'index.html', | ||
| type: 'md', | ||
| pageFile: { filepath: mdFile }, | ||
| }), | ||
| globalVars: { layout: 'default' }, | ||
| globalStyle: undefined, | ||
| globalClient: undefined, | ||
| defaultStyle: null, | ||
| defaultClient: null, | ||
| builderOptions: /** @type {any} */ ({}), | ||
| }) | ||
|
|
||
| await pd.init({ layouts: { default: fakeLayout } }) | ||
|
|
||
| // After init, replace pageVars with a proxy that throws on property access | ||
| // to trigger the catch block in the vars getter. | ||
| const originalError = new Error('getter exploded') | ||
| pd.pageVars = new Proxy({}, { | ||
| ownKeys: () => ['exploding-key'], | ||
| getOwnPropertyDescriptor: () => ({ enumerable: true, configurable: true, writable: true, value: undefined }), | ||
| get (_target, key) { | ||
| if (typeof key === 'symbol') return undefined | ||
| throw originalError | ||
| }, | ||
| }) | ||
|
|
||
| assert.throws( | ||
| () => pd.vars, | ||
| (err) => { | ||
| assert.ok(err instanceof Error, 'throws an Error') | ||
| assert.ok( | ||
| err.message.includes('blog/post'), | ||
| `message should include page path, got: "${err.message}"` | ||
| ) | ||
| assert.ok( | ||
| err.message.includes('getter exploded'), | ||
| `message should include original error message, got: "${err.message}"` | ||
| ) | ||
| assert.strictEqual(err.cause, originalError, 'err.cause should be the original error') | ||
| return true | ||
| } | ||
| ) | ||
| } finally { | ||
| await rm(dir, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or 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 hidden or 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,53 @@ | ||
| import { test } from 'node:test' | ||
| import assert from 'node:assert' | ||
| import { PageData } from './page-data.js' | ||
|
|
||
| test.describe('PageData.vars', () => { | ||
| test('throws with page path before initialization', () => { | ||
| const pd = new PageData({ | ||
| pageInfo: /** @type {any} */ ({ path: 'blog/test-post' }), | ||
|
bcomnes marked this conversation as resolved.
|
||
| globalVars: {}, | ||
| globalStyle: undefined, | ||
| globalClient: undefined, | ||
| defaultStyle: null, | ||
| defaultClient: null, | ||
| builderOptions: /** @type {any} */ ({}), | ||
| }) | ||
|
|
||
| assert.throws( | ||
| () => pd.vars, | ||
| (err) => { | ||
| assert.ok(err instanceof Error, 'throws an Error') | ||
| assert.ok( | ||
| err.message.includes('blog/test-post'), | ||
| `error message should include the page path, got: "${err.message}"` | ||
| ) | ||
| return true | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| test('error message includes unknown page fallback when pageInfo has no path', () => { | ||
| const pd = new PageData({ | ||
| pageInfo: /** @type {any} */ ({}), | ||
| globalVars: {}, | ||
| globalStyle: undefined, | ||
| globalClient: undefined, | ||
| defaultStyle: null, | ||
| defaultClient: null, | ||
| builderOptions: /** @type {any} */ ({}), | ||
| }) | ||
|
|
||
| assert.throws( | ||
| () => pd.vars, | ||
| (err) => { | ||
| assert.ok(err instanceof Error, 'throws an Error') | ||
| assert.ok( | ||
| err.message.includes('<unknown page>'), | ||
| `error message should include fallback text, got: "${err.message}"` | ||
| ) | ||
| return true | ||
| } | ||
| ) | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.