mirror of
https://github.com/status-im/metro.git
synced 2025-02-10 10:07:12 +00:00
ModuleResolution: move InvalidPackageError handler one level up
Summary: In a further diff, I'll add cases where this error may be thrown at a higher level than the `resolveFileOrDir` function. So we need to move the handler and wrapping logic up the stack, that doesn't have any difference in practice. Reviewed By: cpojer Differential Revision: D6642475 fbshipit-source-id: 6b746386a170bfc167a9b50d9786fbb98c920c6d
This commit is contained in:
parent
86eb9680ac
commit
af1e6e3298
@ -338,19 +338,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||
...this._options,
|
||||
getPackageMainPath: this._getPackageMainPath,
|
||||
};
|
||||
let result;
|
||||
try {
|
||||
result = resolveFileOrDir(context, potentialModulePath, platform);
|
||||
} catch (error) {
|
||||
if (error instanceof InvalidPackageError) {
|
||||
throw new PackageResolutionError({
|
||||
packageError: error,
|
||||
originModulePath: fromModule.path,
|
||||
targetModuleName: toModuleName,
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const result = resolveFileOrDir(context, potentialModulePath, platform);
|
||||
if (result.type === 'resolved') {
|
||||
return this._getFileResolvedModule(result.resolution);
|
||||
}
|
||||
@ -484,31 +472,6 @@ function resolvePackage(
|
||||
});
|
||||
}
|
||||
|
||||
class PackageResolutionError extends Error {
|
||||
originModulePath: string;
|
||||
packageError: InvalidPackageError;
|
||||
targetModuleName: string;
|
||||
|
||||
constructor(opts: {|
|
||||
+originModulePath: string,
|
||||
+packageError: InvalidPackageError,
|
||||
+targetModuleName: string,
|
||||
|}) {
|
||||
const perr = opts.packageError;
|
||||
super(
|
||||
`While trying to resolve module \`${opts.targetModuleName}\` from file ` +
|
||||
`\`${opts.originModulePath}\`, the package ` +
|
||||
`\`${perr.packageJsonPath}\` was successfully found. However, ` +
|
||||
`this package itself specifies ` +
|
||||
`a \`main\` module field that could not be resolved (` +
|
||||
`\`${perr.mainPrefixPath}\`. Indeed, none of these files exist:\n\n` +
|
||||
` * \`${formatFileCandidates(perr.fileCandidates)}\`\n` +
|
||||
` * \`${formatFileCandidates(perr.indexCandidates)}\``,
|
||||
);
|
||||
Object.assign(this, opts);
|
||||
}
|
||||
}
|
||||
|
||||
function formatFileCandidates(candidates: FileCandidates): string {
|
||||
if (candidates.type === 'asset') {
|
||||
return candidates.name;
|
||||
@ -791,7 +754,9 @@ class UnableToResolveError extends Error {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
UnableToResolveError,
|
||||
ModuleResolver,
|
||||
formatFileCandidates,
|
||||
InvalidPackageError,
|
||||
isRelativeImport,
|
||||
ModuleResolver,
|
||||
UnableToResolveError,
|
||||
};
|
||||
|
@ -24,7 +24,7 @@ import type {Options as TransformWorkerOptions} from '../../JSTransformer/worker
|
||||
import type {ReadResult, CachedReadResult} from '../Module';
|
||||
import type {ModuleResolver} from './ModuleResolution';
|
||||
|
||||
const {isRelativeImport} = ModuleResolution;
|
||||
const {InvalidPackageError, formatFileCandidates} = ModuleResolution;
|
||||
|
||||
export type Packageish = {
|
||||
isHaste(): boolean,
|
||||
@ -67,6 +67,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
|
||||
_immediateResolutionCache: {[key: string]: TModule, __proto__: null};
|
||||
_options: Options<TModule, TPackage>;
|
||||
static AmbiguousModuleResolutionError: Class<AmbiguousModuleResolutionError>;
|
||||
static PackageResolutionError: Class<PackageResolutionError>;
|
||||
|
||||
constructor(options: Options<TModule, TPackage>) {
|
||||
this._options = options;
|
||||
@ -104,6 +105,13 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
|
||||
if (error instanceof DuplicateHasteCandidatesError) {
|
||||
throw new AmbiguousModuleResolutionError(fromModule.path, error);
|
||||
}
|
||||
if (error instanceof InvalidPackageError) {
|
||||
throw new PackageResolutionError({
|
||||
packageError: error,
|
||||
originModulePath: fromModule.path,
|
||||
targetModuleName: toModuleName,
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@ -138,6 +146,32 @@ class AmbiguousModuleResolutionError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
class PackageResolutionError extends Error {
|
||||
originModulePath: string;
|
||||
packageError: InvalidPackageError;
|
||||
targetModuleName: string;
|
||||
|
||||
constructor(opts: {|
|
||||
+originModulePath: string,
|
||||
+packageError: InvalidPackageError,
|
||||
+targetModuleName: string,
|
||||
|}) {
|
||||
const perr = opts.packageError;
|
||||
super(
|
||||
`While trying to resolve module \`${opts.targetModuleName}\` from file ` +
|
||||
`\`${opts.originModulePath}\`, the package ` +
|
||||
`\`${perr.packageJsonPath}\` was successfully found. However, ` +
|
||||
`this package itself specifies ` +
|
||||
`a \`main\` module field that could not be resolved (` +
|
||||
`\`${perr.mainPrefixPath}\`. Indeed, none of these files exist:\n\n` +
|
||||
` * \`${formatFileCandidates(perr.fileCandidates)}\`\n` +
|
||||
` * \`${formatFileCandidates(perr.indexCandidates)}\``,
|
||||
);
|
||||
Object.assign(this, opts);
|
||||
}
|
||||
}
|
||||
|
||||
ResolutionRequest.AmbiguousModuleResolutionError = AmbiguousModuleResolutionError;
|
||||
ResolutionRequest.PackageResolutionError = PackageResolutionError;
|
||||
|
||||
module.exports = ResolutionRequest;
|
||||
|
Loading…
x
Reference in New Issue
Block a user