Skip to content

Commit

Permalink
refactor: wrapImportError
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Nov 17, 2022
1 parent fd38b1f commit 67475dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
9 changes: 3 additions & 6 deletions node/formats/eszip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { wrapBundleError } from '../bundle_error.js'
import { EdgeFunction } from '../edge_function.js'
import { FeatureFlags } from '../feature_flags.js'
import { ImportMap } from '../import_map.js'
import { checkNpmImportError } from '../npm_import_error.js'
import { wrapNpmImportError } from '../npm_import_error.js'
import { getPackagePath } from '../package_json.js'
import { getFileHash } from '../utils/sha256.js'

Expand Down Expand Up @@ -52,11 +52,8 @@ const bundleESZIP = async ({

try {
await deno.run(['run', ...flags, bundler, JSON.stringify(payload)], { pipeOutput: true })
} catch (_error: unknown) {
let error = _error
error = checkNpmImportError(error) ?? error

throw wrapBundleError(error, { format: 'eszip' })
} catch (error: unknown) {
throw wrapBundleError(wrapNpmImportError(error), { format: 'eszip' })
}

const hash = await getFileHash(destPath)
Expand Down
8 changes: 3 additions & 5 deletions node/formats/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Bundle } from '../bundle.js'
import { wrapBundleError } from '../bundle_error.js'
import { EdgeFunction } from '../edge_function.js'
import { ImportMap } from '../import_map.js'
import { checkNpmImportError } from '../npm_import_error.js'
import { wrapNpmImportError } from '../npm_import_error.js'
import type { FormatFunction } from '../server/server.js'
import { getFileHash } from '../utils/sha256.js'

Expand Down Expand Up @@ -44,10 +44,8 @@ const bundleJS = async ({

try {
await deno.run(['bundle', ...flags, stage2Path, jsBundlePath], { pipeOutput: true })
} catch (_error: unknown) {
let error = _error
error = checkNpmImportError(error) ?? error
throw wrapBundleError(error, { format: 'javascript' })
} catch (error: unknown) {
throw wrapBundleError(wrapNpmImportError(error), { format: 'javascript' })
}

await fs.unlink(stage2Path)
Expand Down
6 changes: 4 additions & 2 deletions node/npm_import_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class NPMImportError extends Error {
}
}

const checkNpmImportError = (input: unknown): NPMImportError | undefined => {
const wrapNpmImportError = (input: unknown) => {
if (input instanceof Error) {
const match = input.message.match(/Relative import path "(.*)" not prefixed with/)
if (match !== null) {
const [, moduleName] = match
return new NPMImportError(input, moduleName)
}
}

return input
}

export { NPMImportError, checkNpmImportError }
export { NPMImportError, wrapNpmImportError }

0 comments on commit 67475dd

Please sign in to comment.