Improve error handling in require-unbundle

Reviewed By: davidaurelio

Differential Revision: D3207450

fb-gh-sync-id: 35247c265e35976dcee9fca4215403efa604479e
fbshipit-source-id: 35247c265e35976dcee9fca4215403efa604479e
This commit is contained in:
Pieter De Baets 2016-04-22 06:17:17 -07:00 committed by Facebook Github Bot 5
parent 9bae30f8b5
commit 8b1726bdad
2 changed files with 16 additions and 11 deletions

View File

@ -31,8 +31,7 @@ RCT_EXPORT_METHOD(getScriptText:(RCTPromiseResolveBlock)resolve
{
if (RCT_DEV && self.scriptData && self.scriptURL) {
NSString *scriptText = [[NSString alloc] initWithData:self.scriptData encoding:NSUTF8StringEncoding];
resolve(@{@"text": scriptText, @"url": self.scriptURL.absoluteString});
resolve(@{@"text": RCTNullIfNil(scriptText), @"url": self.scriptURL.absoluteString});
} else {
reject(RCTErrorUnavailable, nil, RCTErrorWithMessage(@"Source code is not available"));
}

View File

@ -9,15 +9,11 @@
'use strict';
const {ErrorUtils, nativeRequire} = global;
global.require = require;
global.__d = define;
const modules = Object.create(null);
const loadModule = ErrorUtils ?
guardedLoadModule : loadModuleImplementation;
function define(moduleId, factory) {
if (moduleId in modules) {
// prevent repeated calls to `global.nativeRequire` to overwrite modules
@ -36,20 +32,29 @@ function require(moduleId) {
const module = modules[moduleId];
return module && module.isInitialized
? module.exports
: loadModule(moduleId, module);
: guardedLoadModule(moduleId, module);
}
var inGuard = false;
function guardedLoadModule(moduleId, module) {
if (global.ErrorUtils && !inGuard) {
inGuard = true;
var returnValue;
try {
return loadModuleImplementation(moduleId, module);
returnValue = loadModuleImplementation(moduleId, module);
} catch (e) {
ErrorUtils.reportFatalError(e);
global.ErrorUtils.reportFatalError(e);
}
inGuard = false;
return returnValue;
} else {
return loadModuleImplementation(moduleId, module);
}
}
function loadModuleImplementation(moduleId, module) {
if (!module) {
nativeRequire(moduleId);
global.nativeRequire(moduleId);
module = modules[moduleId];
}
@ -90,6 +95,7 @@ function loadModuleImplementation(moduleId, module) {
module.isInitialized = false;
module.hasError = true;
module.exports = undefined;
throw e;
}
}