1 /*
2  * ClientException.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.server;
16 
17 import org.rstudio.core.client.StringUtil;
18 
19 import com.google.gwt.core.client.GWT;
20 import com.google.gwt.core.client.JavaScriptObject;
21 import com.google.gwt.core.client.JsArray;
22 
23 public class ClientException extends JavaScriptObject
24 {
create(Throwable e)25    public static final ClientException create(Throwable e)
26    {
27       JsArray<StackElement> stack = JsArray.createArray().cast();
28       for (StackTraceElement element : e.getStackTrace())
29          stack.push(StackElement.create(element));
30 
31       return create(StringUtil.notNull(e.getMessage()),
32                     GWT.getPermutationStrongName(),
33                     stack);
34    }
35 
create( String message, String strongName, JsArray<StackElement> stack)36    public static native final ClientException create(
37                                                String message,
38                                                String strongName,
39                                                JsArray<StackElement> stack) /*-{
40       var ex = new Object();
41       ex.message = message;
42       ex.strong_name = strongName;
43       ex.stack = stack;
44       return ex;
45    }-*/;
46 
ClientException()47    protected ClientException()
48    {
49    }
50 
getMessage()51    public native final String getMessage() /*-{
52       return this.message;
53    }-*/;
54 
getStrongName()55    public native final String getStrongName() /*-{
56       return this.strong_name;
57    }-*/;
58 
getStack()59    public native final JsArray<StackElement> getStack() /*-{
60       return this.stack;
61    }-*/;
62 
63    public static class StackElement extends JavaScriptObject
64    {
create(StackTraceElement element)65       public static final StackElement create(StackTraceElement element)
66       {
67          return create(StringUtil.notNull(element.getFileName()),
68                        StringUtil.notNull(element.getClassName()),
69                        StringUtil.notNull(element.getMethodName()),
70                        element.getLineNumber());
71       }
72 
create(String fileName, String className, String methodName, int lineNumber)73       public static native final StackElement create(String fileName,
74                                                   String className,
75                                                   String methodName,
76                                                   int lineNumber) /*-{
77          var item = new Object();
78          item.file_name = fileName;
79          item.class_name = className;
80          item.method_name = methodName;
81          item.line_number = lineNumber;
82          return item;
83       }-*/;
84 
StackElement()85       protected StackElement()
86       {
87       }
88 
getFileName()89       public native final String getFileName() /*-{
90          return this.file_name;
91       }-*/;
92 
getClassName()93       public native final String getClassName() /*-{
94          return this.class_name;
95       }-*/;
96 
getMethodName()97       public native final String getMethodName() /*-{
98          return this.method_name;
99       }-*/;
100 
getLineNumber()101       public native final String getLineNumber() /*-{
102          return this.line_number;
103       }-*/;
104    }
105 }
106