Bug 1566307 - Improve the error generated when resolution of a module specifier fails r=hsivonen

This combines the two places where an error was generated for this into one and adds localiation for the message. The message is improved as requested in the bug.

Differential Revision: https://phabricator.services.mozilla.com/D79862
This commit is contained in:
Jon Coppeard 2020-06-16 18:40:17 +00:00
Родитель 1f78f06a5c
Коммит b2762155a5
3 изменённых файлов: 38 добавлений и 20 удалений

Просмотреть файл

@ -324,6 +324,7 @@ ModuleSourceMalformed=Module source URI is malformed: “%S”.
# LOCALIZATION NOTE: Do not translate "<script>".
ScriptSourceNotAllowed=<script> source URI is not allowed in this document: “%S”.
ModuleSourceNotAllowed=Module source URI is not allowed in this document: “%S”.
ModuleResolveFailure=Error resolving module specifier “%S”. Relative module specifiers must start with “./”, “../” or “/”.
# LOCALIZATION NOTE: %1$S is the invalid property value and %2$S is the property name.
InvalidKeyframePropertyValue=Keyframe property value “%1$S” is invalid according to the syntax for “%2$S”.
# LOCALIZATION NOTE: Do not translate "ReadableStream".

Просмотреть файл

@ -621,34 +621,43 @@ nsresult ScriptLoader::CreateModuleScript(ModuleLoadRequest* aRequest) {
return rv;
}
static nsresult HandleResolveFailure(JSContext* aCx, ModuleScript* aScript,
static nsresult HandleResolveFailure(JSContext* aCx, LoadedScript* aScript,
const nsAString& aSpecifier,
uint32_t aLineNumber,
uint32_t aColumnNumber) {
nsAutoCString url;
aScript->BaseURL()->GetAsciiSpec(url);
uint32_t aColumnNumber,
JS::MutableHandle<JS::Value> errorOut) {
JS::Rooted<JSString*> filename(aCx);
filename = JS_NewStringCopyZ(aCx, url.get());
if (aScript) {
nsAutoCString url;
aScript->BaseURL()->GetAsciiSpec(url);
filename = JS_NewStringCopyZ(aCx, url.get());
} else {
filename = JS_NewStringCopyZ(aCx, "(unknown)");
}
if (!filename) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsAutoString message(NS_LITERAL_STRING("Error resolving module specifier: "));
message.Append(aSpecifier);
AutoTArray<nsString, 1> errorParams;
errorParams.AppendElement(aSpecifier);
JS::Rooted<JSString*> string(aCx, JS_NewUCStringCopyZ(aCx, message.get()));
nsAutoString errorText;
nsresult rv = nsContentUtils::FormatLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "ModuleResolveFailure", errorParams,
errorText);
NS_ENSURE_SUCCESS(rv, rv);
JS::Rooted<JSString*> string(aCx, JS_NewUCStringCopyZ(aCx, errorText.get()));
if (!string) {
return NS_ERROR_OUT_OF_MEMORY;
}
JS::Rooted<JS::Value> error(aCx);
if (!JS::CreateError(aCx, JSEXN_TYPEERR, nullptr, filename, aLineNumber,
aColumnNumber, nullptr, string, &error)) {
aColumnNumber, nullptr, string, errorOut)) {
return NS_ERROR_OUT_OF_MEMORY;
}
aScript->SetParseError(error);
return NS_OK;
}
@ -736,9 +745,12 @@ static nsresult ResolveRequestedModules(ModuleLoadRequest* aRequest,
uint32_t columnNumber = 0;
JS::GetRequestedModuleSourcePos(cx, element, &lineNumber, &columnNumber);
nsresult rv =
HandleResolveFailure(cx, ms, specifier, lineNumber, columnNumber);
JS::Rooted<JS::Value> error(cx);
nsresult rv = HandleResolveFailure(cx, ms, specifier, lineNumber,
columnNumber, &error);
NS_ENSURE_SUCCESS(rv, rv);
ms->SetParseError(error);
return NS_ERROR_FAILURE;
}
@ -972,8 +984,8 @@ bool HostImportModuleDynamically(JSContext* aCx,
RefPtr<LoadedScript> script(GetLoadedScriptOrNull(aCx, aReferencingPrivate));
// Attempt to resolve the module specifier.
nsAutoJSString string;
if (!string.init(aCx, aSpecifier)) {
nsAutoJSString specifier;
if (!specifier.init(aCx, aSpecifier)) {
return false;
}
@ -982,10 +994,16 @@ bool HostImportModuleDynamically(JSContext* aCx,
return false;
}
nsCOMPtr<nsIURI> uri = ResolveModuleSpecifier(loader, script, string);
nsCOMPtr<nsIURI> uri = ResolveModuleSpecifier(loader, script, specifier);
if (!uri) {
JS_ReportErrorNumberUC(aCx, js::GetErrorMessage, nullptr,
JSMSG_BAD_MODULE_SPECIFIER, string.get());
JS::Rooted<JS::Value> error(aCx);
nsresult rv = HandleResolveFailure(aCx, script, specifier, 0, 0, &error);
if (NS_FAILED(rv)) {
JS_ReportOutOfMemory(aCx);
return false;
}
JS_SetPendingException(aCx, error);
return false;
}

Просмотреть файл

@ -654,7 +654,6 @@ MSG_DEF(JSMSG_MISSING_NAMESPACE_EXPORT, 0, JSEXN_SYNTAXERR, "export not found f
MSG_DEF(JSMSG_MISSING_EXPORT, 1, JSEXN_SYNTAXERR, "local binding for export '{0}' not found")
MSG_DEF(JSMSG_BAD_MODULE_STATUS, 0, JSEXN_INTERNALERR, "module record has unexpected status")
MSG_DEF(JSMSG_DYNAMIC_IMPORT_FAILED, 0, JSEXN_TYPEERR, "error loading dynamically imported module")
MSG_DEF(JSMSG_BAD_MODULE_SPECIFIER, 1, JSEXN_TYPEERR, "error resolving module specifier '{0}'")
// Promise
MSG_DEF(JSMSG_CANNOT_RESOLVE_PROMISE_WITH_ITSELF, 0, JSEXN_TYPEERR, "A promise cannot be resolved with itself.")