Move package resolution error to its own file

Reviewed By: rafeca

Differential Revision: D7180926

fbshipit-source-id: 24f14a662bbfc776c02093339f571db32af0be90
This commit is contained in:
Peter van der Zee 2018-03-25 13:27:44 -07:00 committed by Facebook Github Bot
parent 94028f16c1
commit 22b5fef24d
5 changed files with 48 additions and 29 deletions

View File

@ -14,6 +14,7 @@
"dependencies": { "dependencies": {
"jest-haste-map": "^22.4.2", "jest-haste-map": "^22.4.2",
"lodash.throttle": "^4.1.1", "lodash.throttle": "^4.1.1",
"metro-resolver": "^0.31.0",
"wordwrap": "^1.0.0" "wordwrap": "^1.0.0"
} }
} }

View File

@ -0,0 +1,41 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const {formatFileCandidates} = require('metro-resolver');
const {InvalidPackageError} = require('metro-resolver');
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);
}
}
module.exports = PackageResolutionError;

View File

@ -11,7 +11,9 @@
'use strict'; 'use strict';
const AmbiguousModuleResolutionError = require('./AmbiguousModuleResolutionError'); const AmbiguousModuleResolutionError = require('./AmbiguousModuleResolutionError');
const PackageResolutionError = require('./PackageResolutionError');
module.exports = { module.exports = {
AmbiguousModuleResolutionError, AmbiguousModuleResolutionError,
PackageResolutionError,
}; };

View File

@ -12,6 +12,7 @@
const AmbiguousModuleResolutionError = require('./errors/AmbiguousModuleResolutionError'); const AmbiguousModuleResolutionError = require('./errors/AmbiguousModuleResolutionError');
const Logger = require('./Logger'); const Logger = require('./Logger');
const PackageResolutionError = require('./errors/PackageResolutionError');
const Terminal = require('./Terminal'); const Terminal = require('./Terminal');
const formatBanner = require('./formatBanner'); const formatBanner = require('./formatBanner');
@ -19,6 +20,7 @@ const formatBanner = require('./formatBanner');
module.exports = { module.exports = {
AmbiguousModuleResolutionError, AmbiguousModuleResolutionError,
Logger, Logger,
PackageResolutionError,
Terminal, Terminal,
formatBanner, formatBanner,
}; };

View File

@ -14,7 +14,8 @@ const path = require('path');
const {AmbiguousModuleResolutionError} = require('metro-core'); const {AmbiguousModuleResolutionError} = require('metro-core');
const {DuplicateHasteCandidatesError} = require('jest-haste-map').ModuleMap; const {DuplicateHasteCandidatesError} = require('jest-haste-map').ModuleMap;
const {formatFileCandidates, InvalidPackageError} = require('metro-resolver'); const {InvalidPackageError} = require('metro-resolver');
const {PackageResolutionError} = require('metro-core');
import type DependencyGraphHelpers from './DependencyGraphHelpers'; import type DependencyGraphHelpers from './DependencyGraphHelpers';
import type {Options as TransformWorkerOptions} from '../../JSTransformer/worker'; import type {Options as TransformWorkerOptions} from '../../JSTransformer/worker';
@ -61,7 +62,6 @@ type Options<TModule, TPackage> = {|
class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> { class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
_immediateResolutionCache: {[key: string]: TModule, __proto__: null}; _immediateResolutionCache: {[key: string]: TModule, __proto__: null};
_options: Options<TModule, TPackage>; _options: Options<TModule, TPackage>;
static PackageResolutionError: Class<PackageResolutionError>;
constructor(options: Options<TModule, TPackage>) { constructor(options: Options<TModule, TPackage>) {
this._options = options; this._options = options;
@ -123,31 +123,4 @@ function getResolutionCacheKey(modulePath, depName) {
return `${path.resolve(modulePath)}:${depName}`; return `${path.resolve(modulePath)}:${depName}`;
} }
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.PackageResolutionError = PackageResolutionError;
module.exports = ResolutionRequest; module.exports = ResolutionRequest;