Don't print a spurious error message when exiting

The dart:fuchsia.exit() call uses Isolate.current.kill() to exit. This
generates a spurious error in the log. This change suppresses that error
by checking if an exit code has been set. The exit code will only be set
right before Isolate.current.kill() is called.

US-362 #done

Change-Id: I291e9669ac9612b85ecb42731dd3d6af87629137
diff --git a/dart_message_handler.cc b/dart_message_handler.cc
index c8fca2c..2a5662e 100644
--- a/dart_message_handler.cc
+++ b/dart_message_handler.cc
@@ -87,7 +87,15 @@
   } else {
     // We are processing messages normally.
     result = Dart_HandleMessages();
-    error = LogIfError(result);
+    // If the Dart program has set a return code, then it is intending to shut
+    // down by way of a fatal error, and so there is no need to emit a log
+    // message.
+    if (dart_state->has_set_return_code() && Dart_IsError(result) &&
+        Dart_IsFatalError(result)) {
+      error = true;
+    } else {
+      error = LogIfError(result);
+    }
   }
 
   if (error) {
diff --git a/dart_state.cc b/dart_state.cc
index 51cdc99..4fb5b76 100644
--- a/dart_state.cc
+++ b/dart_state.cc
@@ -48,6 +48,7 @@
   if (set_return_code_callback_) {
     set_return_code_callback_(return_code);
   }
+  has_set_return_code_ = true;
 }
 
 void DartState::SetReturnCodeCallback(std::function<void(uint32_t)> callback) {
diff --git a/dart_state.h b/dart_state.h
index d9545a6..0550505 100644
--- a/dart_state.h
+++ b/dart_state.h
@@ -53,6 +53,7 @@
 
   void SetReturnCode(uint32_t return_code);
   void SetReturnCodeCallback(std::function<void(uint32_t)> callback);
+  bool has_set_return_code() const { return has_set_return_code_; }
 
   virtual void DidSetIsolate();
 
@@ -66,6 +67,7 @@
   std::unique_ptr<DartMessageHandler> message_handler_;
   std::unique_ptr<FileLoader> file_loader_;
   std::function<void(uint32_t)> set_return_code_callback_;
+  bool has_set_return_code_;
 
  protected:
   fxl::WeakPtrFactory<DartState> weak_factory_;