/*
 * ApplicationUncaughtExceptionHandler.java
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.application;

import org.rstudio.core.client.CsvWriter;
import org.rstudio.studio.client.server.LogEntryType;
import org.rstudio.studio.client.server.Server;
import org.rstudio.studio.client.server.VoidServerRequestCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class ApplicationUncaughtExceptionHandler 
                                    implements UncaughtExceptionHandler
{
   @Inject
   public ApplicationUncaughtExceptionHandler(Server server)
   {
      server_ = server;
   }

   public void register()
   {
      // set uncaught exception handler (first save default so we can call it)
      defaultUncaughtExceptionHandler_ = GWT.getUncaughtExceptionHandler();
      GWT.setUncaughtExceptionHandler(this);
   }
   
   public void onUncaughtException(Throwable e)
   {     
      try
      {
         // call the default handler if there is one
         if (defaultUncaughtExceptionHandler_ != null)
            defaultUncaughtExceptionHandler_.onUncaughtException(e);
         
         // NOTE: we use use | as the logical line delimiter because server log
         // entries cannont contain newlines)
         
         // uncaught exception
         StringBuilder message = new StringBuilder();
         message.append("Uncaught Exception: ");

         CsvWriter csv = new CsvWriter();
         csv.writeValue(GWT.getPermutationStrongName());
         csv.writeValue(e.toString());

         StringBuilder stackTrace = new StringBuilder();
         writeStackTrace(e, stackTrace, false);

         csv.writeValue(stackTrace.toString());

         message.append(csv.getValue());
         
         // log to server
         server_.log(LogEntryType.ERROR, 
                     message.toString(),
                     new VoidServerRequestCallback());
      }
      catch(Throwable throwable)
      {
         // make sure exceptions never escape the uncaught handler
      }
   }

   private void writeStackTrace(Throwable e,
                                StringBuilder stackTrace,
                                boolean includeMessage)
   {
      if (e == null)
         return;

      if (includeMessage)
         stackTrace.append("\n").append(e.toString()).append("\n");

      // stack frame
      StackTraceElement[] stack = e.getStackTrace();
      if (stack != null)
      {
         for (int i=0; i<stack.length; i++)
         {
            if (i > 0)
               stackTrace.append("\n");
            stackTrace.append("    at ");
            stackTrace.append(stack[i].toString());
         }
      }

      if (e instanceof UmbrellaException)
      {
         UmbrellaException ue = (UmbrellaException)e;
         for (Throwable t : ue.getCauses())
            writeStackTrace(t, stackTrace, true);
      }
   }

   
   private final Server server_;
   private UncaughtExceptionHandler defaultUncaughtExceptionHandler_ = null;
}

@@ -1479,21 +1479,128 @@
  e)%0A   %7B
+%0A
      
+ logException(e, %22Uncaught Exception%22);%0A   %7D%0A%0A   private void logException(Throwable e, String label)%0A   %7B
 %0A      t
@@ -2042,35 +2042,31 @@
 .append(
-%22Uncaught Exception
+label).append(%22
 : %22);%0A%0A 
@@ -2568,24 +2568,281 @@
 Callback());
+%0A%0A         if (e instanceof UmbrellaException)%0A         %7B%0A            UmbrellaException ue = (UmbrellaException)e;%0A            for (Throwable t : ue.getCauses())%0A               if (t != null)%0A                  logException(t, %22Nested Exception%22);%0A         %7D
 %0A      %7D%0A   
@@ -3573,24 +3573,24 @@
 oString());%0A
+
          %7D%0A 
@@ -3599,215 +3599,8 @@
    %7D
-%0A%0A      if (e instanceof UmbrellaException)%0A      %7B%0A         UmbrellaException ue = (UmbrellaException)e;%0A         for (Throwable t : ue.getCauses())%0A            writeStackTrace(t, stackTrace, true);%0A      %7D
 %0A   

Stop log truncation of UmbrellaException causes