Skip to content

Commit

Permalink
Add improved errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 25, 2023
1 parent 19a2c08 commit 8281ec4
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 28 deletions.
47 changes: 27 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ import {nanoid} from 'nanoid'
import {removePosition} from 'unist-util-remove-position'
import {VFile} from 'vfile'
import {findUp} from 'vfile-find-up'
import {VFileMessage} from 'vfile-message'

const exec = promisify(execCallback)

Expand Down Expand Up @@ -241,28 +242,20 @@ async function findPackage(from) {

if (!file) return

let doc = ''

try {
doc = String(await fs.readFile(file.path))
/* c8 ignore next 7 -- verbose to test. */
} catch (error) {
// To do: improve error.
const exception = /** @type {NodeJS.ErrnoException} */ (error)
if (exception.code !== 'ENOENT') {
throw new Error('Could not read package: ' + exception)
}
}
const doc = String(await fs.readFile(file.path))

/** @type {PackageJson} */
let value

try {
value = JSON.parse(doc)
} catch (error) {
// To do: improve error.
// Invalid JSON.
throw new Error('Could not parse package: ' + error)
const cause = /** @type {Error} */ (error)
throw new VFileMessage('Cannot parse `package.json` as JSON', {
cause,
ruleId: 'package-json-invalid',
source: 'remark-usage'
})
}

return {value, file}
Expand All @@ -279,8 +272,13 @@ async function findExample(cwd, givenExample) {
: findImplicitExample(cwd)

if (!example) {
// To do: improve error.
throw new Error('Could not find example')
throw new VFileMessage(
'Cannot find example file to use, either pass `options.example` or use a name',
{
ruleId: 'example-missing',
source: 'remark-usage'
}
)
}

const url = new URL(example)
Expand Down Expand Up @@ -360,8 +358,12 @@ async function instrumentExample(cwd, options) {
sourceType: 'unambiguous'
})
} catch (error) {
// To do: improve error.
throw new Error('Could not parse example: ' + error)
const cause = /** @type {Error} */ (error)
throw new VFileMessage('Cannot parse example as JS with Babel', {
cause,
ruleId: 'example-invalid-babel',
source: 'remark-usage'
})
}

let index = -1
Expand Down Expand Up @@ -486,7 +488,12 @@ async function run(example, instrumented, id) {
const result = await exec([process.execPath, filePath].join(' '))
stdout = result.stdout
} catch (error) {
throw new Error('Could not run example: ' + error)
const cause = /** @type {Error} */ (error)
throw new VFileMessage('Cannot run example with Node', {
cause,
ruleId: 'example-invalid-node',
source: 'remark-usage'
})
} finally {
await fs.unlink(filePath)
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"nanoid": "^5.0.0",
"unist-util-remove-position": "^5.0.0",
"vfile": "^6.0.0",
"vfile-find-up": "^7.0.0"
"vfile-find-up": "^7.0.0",
"vfile-message": "^4.0.0"
},
"devDependencies": {
"@types/babel__core": "^7.0.0",
Expand Down
10 changes: 3 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ test('fixtures', async function (t) {
const packageUrl = new URL('../package.json', import.meta.url)
const packageBackUrl = new URL('../package.json.bak', import.meta.url)
const brokenPackageUrl = new URL(
'fail-could-not-parse-package/package.json',
root
)
const brokenExampleUrl = new URL(
'fail-could-not-parse-example/example.js',
'fail-cannot-parse-package/package.json',
root
)
const brokenExampleUrl = new URL('fail-cannot-parse-example/example.js', root)

await fs.writeFile(brokenExampleUrl, '"\n')
await fs.writeFile(brokenPackageUrl, '{\n')
Expand Down Expand Up @@ -90,8 +87,7 @@ test('fixtures', async function (t) {
}

const message = folder.slice(5).replace(/-/g, ' ')
// .replace(/`/g, '')
assert.match(String(error), new RegExp(message, 'i'))
assert.match(String(error).replace(/`/g, ''), new RegExp(message, 'i'))
}
})
}
Expand Down

0 comments on commit 8281ec4

Please sign in to comment.