Skip to content

Commit

Permalink
fix: Error message handling #137 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad authored and guybedford committed Apr 8, 2023
1 parent c992a9f commit 778a835
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"cac": "^6.7.14",
"esbuild": "^0.16.10",
"eslint": "^8.30.0",
"tinyspy": "^1.0.2",
"tsx": "^3.12.1",
"typescript": "^4.9.4"
}
Expand Down
13 changes: 7 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import install from './install'
import link from './link'
import uninstall from './uninstall'
import update from './update'
import { wrapCommandAndRemoveStack } from './utils'

const cli = cac('jspm')

Expand All @@ -25,32 +26,32 @@ cli
cli
.command('install [...packages]', 'install packages')
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map')
.action(install)
.action(wrapCommandAndRemoveStack(install))

cli
.command('update [...packages]', 'update packages')
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map')
.action(update)
.action(wrapCommandAndRemoveStack(update))

cli
.command('uninstall [...packages]', 'remove packages')
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map')
.action(uninstall)
.action(wrapCommandAndRemoveStack(uninstall))

cli
.command('link [...modules]', 'trace install modules')
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map')
.action(link)
.action(wrapCommandAndRemoveStack(link))

cli
.command('inject <htmlFile> [...packages]', 'inject the import map into the provided HTML source')
.option('-o, --output <outputFile>', '.html file for the output html with the import-map')
.action(inject)
.action(wrapCommandAndRemoveStack(inject))

cli
.command('extract [...packages]', 'extract packages from the import map')
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map')
.action(extract)
.action(wrapCommandAndRemoveStack(extract))

cli
.command('')
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ export class JspmError extends Error {
jspmError = true
}

export function wrapCommandAndRemoveStack(fn: Function) {
return async (...args: any[]) => {
try {
await fn(...args)
}
catch (e) {
stopLoading()
process.exitCode = 1
if (e instanceof JspmError || e?.jspmError) {
console.error(`ERR: ${e.message}`)
return
}
throw e
}
}
}

export async function writeMap(
map: IImportMapFile,
flags: Flags,
Expand Down
20 changes: 20 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assert from 'assert'
import { spyOn } from 'tinyspy'
import install from '../src/install'
import { wrapCommandAndRemoveStack } from '../src/utils'

{
let errorStr = ''
spyOn(console, 'error', (err) => {
errorStr = err
})
/* basic install 404 with wrapCommandAndRemoveStack should not throw with stack mesage */
await wrapCommandAndRemoveStack(install)(['package-does-not-exist'], {
env: 'development',
stdout: true,
map: 'test/importmap.json',
})
assert.ok(process.exitCode === 1)
assert.ok(errorStr.includes('Unable to resolve npm:package-does-not-exist@ to a valid version imported from'))
process.exitCode = 0
}

0 comments on commit 778a835

Please sign in to comment.