Bug 1436110 - Fix rendering of thrown string in evaluation results; r=bgrins. draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Wed, 07 Feb 2018 09:42:13 +0100
changeset 751967 90a95af6bf7f5046e652b02263ae118fc4156693
parent 751476 f1a4b64f19b0e93c49492735db30a5023e624ae7
child 751968 2eb0fc46eb2b44acb9c1d3794f38d89e4628cb75
push id98110
push userbmo:nchevobbe@mozilla.com
push dateWed, 07 Feb 2018 08:45:22 +0000
reviewersbgrins
bugs1436110
milestone60.0a1
Bug 1436110 - Fix rendering of thrown string in evaluation results; r=bgrins. If the user tried to evaluate `throw ""`, an "undefined" message was displayed in the console output, which is wrong. Some changes needed to be made to the messages util to better handle those cases. MozReview-Commit-ID: Is5pJYB2N48
devtools/client/webconsole/new-console-output/components/message-types/EvaluationResult.js
devtools/client/webconsole/new-console-output/utils/messages.js
--- a/devtools/client/webconsole/new-console-output/components/message-types/EvaluationResult.js
+++ b/devtools/client/webconsole/new-console-output/components/message-types/EvaluationResult.js
@@ -39,17 +39,17 @@ function EvaluationResult(props) {
     exceptionDocURL,
     frame,
     timeStamp,
     parameters,
     notes,
   } = message;
 
   let messageBody;
-  if (message.messageText) {
+  if (typeof message.messageText !== "undefined" && message.messageText !== null) {
     if (typeof message.messageText === "string") {
       messageBody = message.messageText;
     } else if (
       typeof message.messageText === "object"
       && message.messageText.type === "longString"
     ) {
       messageBody = `${message.messageText.initial}…`;
     }
--- a/devtools/client/webconsole/new-console-output/utils/messages.js
+++ b/devtools/client/webconsole/new-console-output/utils/messages.js
@@ -238,44 +238,51 @@ function transformNetworkEventPacket(pac
     method: networkEvent.request.method,
     updates: networkEvent.updates,
     cause: networkEvent.cause,
   });
 }
 
 function transformEvaluationResultPacket(packet) {
   let {
-    exceptionMessage: messageText,
+    exceptionMessage,
     exceptionDocURL,
+    exception,
     frame,
     result,
     helperResult,
     timestamp: timeStamp,
     notes,
   } = packet;
 
   const parameter = helperResult && helperResult.object
     ? helperResult.object
     : result;
 
   if (helperResult && helperResult.type === "error") {
     try {
-      messageText = l10n.getStr(helperResult.message);
+      exceptionMessage = l10n.getStr(helperResult.message);
     } catch (ex) {
-      messageText = helperResult.message;
+      exceptionMessage = helperResult.message;
     }
+  } else if (typeof exception === "string") {
+    // Wrap thrown strings in Error objects, so `throw "foo"` outputs "Error: foo"
+    exceptionMessage = new Error(exceptionMessage).toString();
   }
 
-  const level = messageText ? MESSAGE_LEVEL.ERROR : MESSAGE_LEVEL.LOG;
+  const level = typeof exceptionMessage !== "undefined" && exceptionMessage !== null
+    ? MESSAGE_LEVEL.ERROR
+    : MESSAGE_LEVEL.LOG;
+
   return new ConsoleMessage({
     source: MESSAGE_SOURCE.JAVASCRIPT,
     type: MESSAGE_TYPE.RESULT,
     helperType: helperResult ? helperResult.type : null,
     level,
-    messageText,
+    messageText: exceptionMessage,
     parameters: [parameter],
     exceptionDocURL,
     frame,
     timeStamp,
     notes,
   });
 }