1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
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.ButtonEnumerations.OkEnum; |
24 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.OkGroup; |
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.Window; |
30 | |
import com.google.gwt.user.client.ui.SimplePanel; |
31 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
32 | |
|
33 | 0 | public class KSErrorDialog { |
34 | |
|
35 | 0 | final static ApplicationContext context = Application.getApplicationContext(); |
36 | |
|
37 | 0 | public enum MessagesRequired { |
38 | 0 | DIALOG_TITLE("errorDialogTitle"), |
39 | 0 | DESCRIBE_ACTION("describeAction"), |
40 | 0 | ERROR_DESCRIPTION("errorDescription"); |
41 | |
|
42 | |
private String messageId; |
43 | 0 | private MessagesRequired(String messageId) { |
44 | 0 | this.messageId = messageId; |
45 | 0 | } |
46 | |
public String toString() { |
47 | 0 | return this.messageId; |
48 | |
} |
49 | |
} |
50 | |
|
51 | |
public static void bindUncaughtExceptionHandler() { |
52 | 0 | GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() { |
53 | |
public void onUncaughtException(Throwable e) { |
54 | 0 | GWT.log(e.getMessage(), e); |
55 | 0 | Window.alert("Uncaught exception was thrown:"+getStackTrace(e)+"\nMessage:"+e.getMessage()); |
56 | 0 | KSErrorDialog.show(e); |
57 | 0 | } |
58 | |
}); |
59 | 0 | } |
60 | |
|
61 | |
private static String getStackTrace(Throwable t){ |
62 | 0 | StringBuilder sb = new StringBuilder(); |
63 | 0 | appendStackTrace(t,sb); |
64 | 0 | return sb.toString(); |
65 | |
} |
66 | |
|
67 | |
private static void appendStackTrace(Throwable t, StringBuilder s) { |
68 | 0 | s.append(t.toString()); |
69 | 0 | s.append(": at\n"); |
70 | 0 | StackTraceElement[] stack = t.getStackTrace(); |
71 | 0 | for (StackTraceElement frame : stack) { |
72 | 0 | s.append(frame.toString()); |
73 | 0 | s.append("\n"); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
public static void show(final Throwable error) { |
78 | 0 | final KSLightBox lightbox = new KSLightBox(); |
79 | |
|
80 | 0 | final VerticalPanel panel = new VerticalPanel(); |
81 | 0 | panel.addStyleName("KS-Error-Dialog"); |
82 | |
|
83 | 0 | final KSLabel title = new KSLabel(context.getMessage(MessagesRequired.DIALOG_TITLE.toString())); |
84 | 0 | title.addStyleName("KS-Error-Dialog-Title"); |
85 | |
|
86 | 0 | final KSLabel errorDescriptionLabel = new KSLabel(context.getMessage(MessagesRequired.ERROR_DESCRIPTION.toString())); |
87 | 0 | errorDescriptionLabel.addStyleName("KS-Error-Dialog-Label"); |
88 | |
|
89 | 0 | final SimplePanel errorDescriptionPanel = new SimplePanel(); |
90 | 0 | errorDescriptionPanel.addStyleName("KS-Error-Dialog-Panel"); |
91 | |
|
92 | 0 | final KSTextArea errorDescription = new KSTextArea(); |
93 | 0 | errorDescription.setText(getErrorDescription(error)); |
94 | 0 | errorDescription.addStyleName("KS-Error-Dialog-TextArea"); |
95 | 0 | errorDescription.setReadOnly(true); |
96 | |
|
97 | 0 | errorDescriptionPanel.add(errorDescription); |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | 0 | final OkGroup buttonPanel = new OkGroup(new Callback<OkEnum>(){ |
110 | |
|
111 | |
@Override |
112 | |
public void exec(OkEnum result) { |
113 | 0 | switch(result){ |
114 | |
case Ok: |
115 | 0 | DeferredCommand.addCommand(new Command() { |
116 | |
public void execute() { |
117 | 0 | sendReport(error); |
118 | 0 | lightbox.hide(); |
119 | 0 | } |
120 | |
}); |
121 | |
break; |
122 | |
} |
123 | 0 | } |
124 | |
}); |
125 | |
|
126 | 0 | panel.add(title); |
127 | 0 | panel.add(errorDescriptionLabel); |
128 | 0 | panel.add(errorDescriptionPanel); |
129 | |
|
130 | |
|
131 | 0 | panel.add(buttonPanel); |
132 | |
|
133 | 0 | panel.setSize("100", "100"); |
134 | |
|
135 | 0 | lightbox.setWidget(panel); |
136 | 0 | lightbox.setSize(440, 200); |
137 | 0 | lightbox.show(); |
138 | |
|
139 | 0 | } |
140 | |
|
141 | |
private static String getErrorDescription(Throwable error) { |
142 | |
|
143 | 0 | return error.getMessage(); |
144 | |
} |
145 | |
|
146 | |
private static void sendReport(final Throwable error) { |
147 | |
|
148 | 0 | Logger.getClientContextInfo().put("logType", "clientError"); |
149 | |
|
150 | 0 | Logger.error("Uncaught exception", error); |
151 | 0 | Logger.sendLogs(); |
152 | |
|
153 | |
|
154 | 0 | Logger.getClientContextInfo().clear(); |
155 | 0 | } |
156 | |
|
157 | |
} |