View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.widgets;
17  
18  
19  import org.kuali.student.common.ui.client.application.Application;
20  import org.kuali.student.common.ui.client.application.ApplicationContext;
21  import org.kuali.student.common.ui.client.logging.Logger;
22  import org.kuali.student.common.ui.client.mvc.Callback;
23  import org.kuali.student.common.ui.client.widgets.buttongroups.OkGroup;
24  import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.OkEnum;
25  
26  import com.google.gwt.core.client.GWT;
27  import com.google.gwt.user.client.Command;
28  import com.google.gwt.user.client.DeferredCommand;
29  import com.google.gwt.user.client.ui.SimplePanel;
30  import com.google.gwt.user.client.ui.VerticalPanel;
31  
32  public class KSErrorDialog {
33  
34      final static ApplicationContext context = Application.getApplicationContext();
35  
36      public enum MessagesRequired {
37          DIALOG_TITLE("errorDialogTitle"),
38          DESCRIBE_ACTION("describeAction"),
39          ERROR_DESCRIPTION("errorDescription");
40  
41          private String messageId;
42          private MessagesRequired(String messageId) {
43              this.messageId = messageId;
44          }
45          public String toString() {
46              return this.messageId;
47          }
48      }
49  
50      public static void bindUncaughtExceptionHandler() {
51          GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
52              public void onUncaughtException(Throwable e) {
53                  GWT.log(e.getMessage(), e);
54                  //Window.alert("Uncaught exception was thrown:"+getStackTrace(e)+"\nMessage:"+e.getMessage());
55              	KSErrorDialog.show(e);
56              }
57          });
58      }
59  
60      private static String getStackTrace(Throwable t){
61      	StringBuilder sb =  new StringBuilder();
62      	appendStackTrace(t,sb);
63      	return sb.toString();
64      }
65      
66      private static void appendStackTrace(Throwable t, StringBuilder s) {
67          s.append(t.toString());
68          s.append(": at\n");
69          StackTraceElement[] stack = t.getStackTrace();
70          for (StackTraceElement frame : stack) {
71              s.append(frame.toString());
72              s.append("\n");
73          }
74      }
75      
76      public static void show(final Throwable error) {
77          final KSLightBox lightbox = new KSLightBox();
78  
79          final VerticalPanel panel = new VerticalPanel();
80          panel.addStyleName("KS-Error-Dialog");
81  
82          final KSLabel title = new KSLabel(context.getMessage(MessagesRequired.DIALOG_TITLE.toString()));
83          title.addStyleName("KS-Error-Dialog-Title");
84  
85          final KSLabel errorDescriptionLabel = new KSLabel(context.getMessage(MessagesRequired.ERROR_DESCRIPTION.toString()));
86          errorDescriptionLabel.addStyleName("KS-Error-Dialog-Label");
87  
88          final SimplePanel errorDescriptionPanel = new SimplePanel();
89          errorDescriptionPanel.addStyleName("KS-Error-Dialog-Panel");
90  
91          final KSTextArea errorDescription = new KSTextArea();
92          errorDescription.setText(getErrorDescription(error));
93          errorDescription.addStyleName("KS-Error-Dialog-TextArea");
94          errorDescription.setReadOnly(true);
95          //errorDescription.setEnabled(false);
96          errorDescriptionPanel.add(errorDescription);
97  
98  /*        final KSLabel describeActionLabel = new KSLabel(context.getMessage(MessagesRequired.DESCRIBE_ACTION.toString()));
99          describeActionLabel.addStyleName(KSStyles.KS_ERROR_DIALOG_LABEL);
100 
101         final SimplePanel actionDescriptionPanel = new SimplePanel();
102         actionDescriptionPanel.addStyleName(KSStyles.KS_ERROR_DIALOG_PANEL);
103 
104         final KSTextArea actionDescription = new KSTextArea();
105         actionDescription.addStyleName(KSStyles.KS_ERROR_DIALOG_TEXTAREA);
106         actionDescriptionPanel.add(actionDescription);
107 */
108         final OkGroup buttonPanel = new OkGroup(new Callback<OkEnum>(){
109 
110             @Override
111             public void exec(OkEnum result) {
112                 switch(result){
113                     case Ok:
114                         DeferredCommand.addCommand(new Command() {
115                             public void execute() {
116                                 sendReport(error/*, actionDescription.getText()*/);
117                                 lightbox.hide();
118                             }
119                         });
120                         break;
121                 }
122             }
123         });
124 
125         lightbox.setNonCaptionHeader(title);
126         panel.add(errorDescriptionLabel);
127         panel.add(errorDescriptionPanel);
128 //        panel.add(describeActionLabel);
129 //        panel.add(actionDescriptionPanel);
130         panel.add(buttonPanel);
131 
132         panel.setSize("100", "100");
133 
134         lightbox.setWidget(panel);
135         lightbox.setSize(440, 200);
136         lightbox.show();
137 
138     }
139 
140     private static String getErrorDescription(Throwable error) {
141         // TODO maybe retrieve more error info
142         return error.getMessage();
143     }
144 
145     private static void sendReport(final Throwable error/*, final String actionDescription*/) {
146         // TODO actually gather client context info, such as browser version, user id, etc
147         Logger.getClientContextInfo().put("logType", "clientError");
148 //        Logger.getClientContextInfo().put("actionDescription", actionDescription);
149         Logger.error("Uncaught exception", error);
150         Logger.sendLogs();
151 
152         // hack, clear out context info, should probably find a better way of doing this
153         Logger.getClientContextInfo().clear();
154     }
155 
156 }