mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
Better NotFound error handling in CodeFetch
throwResolutionError was swallowing unrelated errors.
This commit is contained in:
parent
8090fb252b
commit
e2a285b871
4 changed files with 42 additions and 54 deletions
|
@ -117,20 +117,6 @@ export class ModuleMetaData implements ts.IScriptSnapshot {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw a module resolution error, when a module is unsuccessfully resolved.
|
||||
*/
|
||||
function throwResolutionError(
|
||||
message: string,
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): never {
|
||||
throw new Error(
|
||||
// tslint:disable-next-line:max-line-length
|
||||
`Cannot resolve module "${moduleSpecifier}" from "${containingFile}".\n ${message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A singleton class that combines the TypeScript Language Service host API
|
||||
* with Deno specific APIs to provide an interface for compiling and running
|
||||
|
@ -514,9 +500,9 @@ export class DenoCompiler
|
|||
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
||||
return this._moduleMetaDataMap.get(fileName)!;
|
||||
}
|
||||
let moduleId: ModuleId = "";
|
||||
let sourceCode: SourceCode | undefined;
|
||||
let outputCode: OutputCode | undefined;
|
||||
let moduleId: ModuleId;
|
||||
let sourceCode: SourceCode;
|
||||
let outputCode: OutputCode | null;
|
||||
if (
|
||||
moduleSpecifier.startsWith(ASSETS) ||
|
||||
containingFile.startsWith(ASSETS)
|
||||
|
@ -524,38 +510,24 @@ export class DenoCompiler
|
|||
// Assets are compiled into the runtime javascript bundle.
|
||||
// we _know_ `.pop()` will return a string, but TypeScript doesn't so
|
||||
// not null assertion
|
||||
const moduleId = moduleSpecifier.split("/").pop()!;
|
||||
moduleId = moduleSpecifier.split("/").pop()!;
|
||||
const assetName = moduleId.includes(".") ? moduleId : `${moduleId}.d.ts`;
|
||||
assert(assetName in assetSourceCode, `No such asset "${assetName}"`);
|
||||
sourceCode = assetSourceCode[assetName];
|
||||
fileName = `${ASSETS}/${assetName}`;
|
||||
outputCode = "";
|
||||
} else {
|
||||
// We query Rust with a CodeFetch message. It will load the sourceCode,
|
||||
// and if there is any outputCode cached, will return that as well.
|
||||
let fetchResponse;
|
||||
try {
|
||||
fetchResponse = this._os.codeFetch(moduleSpecifier, containingFile);
|
||||
} catch (e) {
|
||||
return throwResolutionError(
|
||||
`os.codeFetch message: ${e.message}`,
|
||||
moduleSpecifier,
|
||||
containingFile
|
||||
);
|
||||
}
|
||||
moduleId = fetchResponse.moduleName || "";
|
||||
fileName = fetchResponse.filename || undefined;
|
||||
sourceCode = fetchResponse.sourceCode || undefined;
|
||||
outputCode = fetchResponse.outputCode || undefined;
|
||||
}
|
||||
if (!sourceCode || sourceCode.length === 0 || !fileName) {
|
||||
return throwResolutionError(
|
||||
"Invalid source code or file name.",
|
||||
moduleSpecifier,
|
||||
containingFile
|
||||
);
|
||||
const fetchResponse = this._os.codeFetch(moduleSpecifier, containingFile);
|
||||
moduleId = fetchResponse.moduleName!;
|
||||
fileName = fetchResponse.filename!;
|
||||
sourceCode = fetchResponse.sourceCode!;
|
||||
outputCode = fetchResponse.outputCode!;
|
||||
}
|
||||
assert(sourceCode!.length > 0);
|
||||
this._log("resolveModule sourceCode length:", sourceCode.length);
|
||||
this._log("resolveModule has outputCode:", !!outputCode);
|
||||
this._log("resolveModule has outputCode:", outputCode! != null);
|
||||
this._setFileName(moduleSpecifier, containingFile, fileName);
|
||||
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
||||
return this._moduleMetaDataMap.get(fileName)!;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
use errors::DenoError;
|
||||
use errors::DenoResult;
|
||||
use errors::ErrorKind;
|
||||
use fs as deno_fs;
|
||||
use net;
|
||||
use ring;
|
||||
|
@ -159,7 +160,7 @@ impl DenoDir {
|
|||
let (module_name, filename) =
|
||||
self.resolve_module(module_specifier, containing_file)?;
|
||||
|
||||
let out = self
|
||||
let result = self
|
||||
.get_source_code(module_name.as_str(), filename.as_str())
|
||||
.and_then(|source_code| {
|
||||
Ok(CodeFetchOutput {
|
||||
|
@ -168,7 +169,24 @@ impl DenoDir {
|
|||
source_code,
|
||||
maybe_output_code: None,
|
||||
})
|
||||
})?;
|
||||
});
|
||||
let out = match result {
|
||||
Err(err) => {
|
||||
if err.kind() == ErrorKind::NotFound {
|
||||
// For NotFound, change the message to something better.
|
||||
return Err(DenoError::from(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
format!(
|
||||
"Cannot resolve module \"{}\" from \"{}\"",
|
||||
module_specifier, containing_file
|
||||
),
|
||||
)));
|
||||
} else {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
Ok(out) => out,
|
||||
};
|
||||
|
||||
let result =
|
||||
self.load_cache(out.filename.as_str(), out.source_code.as_str());
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
Error: Cannot resolve module "bad-module.ts" from "[WILDCARD]error_004_missing_module.ts".
|
||||
os.codeFetch message: [WILDCARD] (os error 2)
|
||||
at throwResolutionError (deno/js/compiler.ts:[WILDCARD])
|
||||
NotFound: Cannot resolve module "bad-module.ts" from "[WILDCARD]/tests/error_004_missing_module.ts"
|
||||
at maybeError (deno/js/errors.ts:[WILDCARD])
|
||||
at maybeThrowError (deno/js/errors.ts:[WILDCARD])
|
||||
at send (deno/js/fbs_util.ts:[WILDCARD])
|
||||
at Object.codeFetch (deno/js/os.ts:[WILDCARD])
|
||||
at DenoCompiler.resolveModule (deno/js/compiler.ts:[WILDCARD])
|
||||
at DenoCompiler._resolveModuleName (deno/js/compiler.ts:[WILDCARD])
|
||||
at moduleNames.map.name (deno/js/compiler.ts:[WILDCARD])
|
||||
at Array.map (<anonymous>)
|
||||
at DenoCompiler.resolveModuleNames (deno/js/compiler.ts:[WILDCARD])
|
||||
at Object.compilerHost.resolveModuleNames (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
at resolveModuleNamesWorker (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
at resolveModuleNamesReusingOldState (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
at processImportedModules (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
Error: Cannot resolve module "bad-module.ts" from "[WILDCARD]deno/tests/error_005_missing_dynamic_import.ts".
|
||||
os.codeFetch message: [WILDCARD] (os error 2)
|
||||
at throwResolutionError (deno/js/compiler.ts:[WILDCARD])
|
||||
NotFound: Cannot resolve module "bad-module.ts" from "[WILDCARD]deno/tests/error_005_missing_dynamic_import.ts"
|
||||
at maybeError (deno/js/errors.ts:[WILDCARD])
|
||||
at maybeThrowError (deno/js/errors.ts:[WILDCARD])
|
||||
at send (deno/js/fbs_util.ts:[WILDCARD])
|
||||
at Object.codeFetch (deno/js/os.ts:[WILDCARD])
|
||||
at DenoCompiler.resolveModule (deno/js/compiler.ts:[WILDCARD])
|
||||
at DenoCompiler._resolveModuleName (deno/js/compiler.ts:[WILDCARD])
|
||||
at moduleNames.map.name (deno/js/compiler.ts:[WILDCARD])
|
||||
at Array.map (<anonymous>)
|
||||
at DenoCompiler.resolveModuleNames (deno/js/compiler.ts:[WILDCARD])
|
||||
at Object.compilerHost.resolveModuleNames (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
at resolveModuleNamesWorker (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
at resolveModuleNamesReusingOldState (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
at processImportedModules (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])
|
||||
|
|
Loading…
Reference in a new issue