1 /*
2  * RemoteServerError.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 
16 package org.rstudio.studio.client.server.remote;
17 
18 import com.google.gwt.json.client.JSONNull;
19 import com.google.gwt.json.client.JSONValue;
20 import org.rstudio.core.client.jsonrpc.RpcError;
21 import org.rstudio.core.client.jsonrpc.RpcUnderlyingError;
22 import org.rstudio.studio.client.server.ServerError;
23 import org.rstudio.studio.client.server.ServerErrorCause;
24 
25 class RemoteServerError implements ServerError
26 {
RemoteServerError(RpcError rpcError)27    public RemoteServerError(RpcError rpcError)
28    {
29       code_ = codeFromRpcErrorCode(rpcError.getCode());
30       message_ = rpcError.getMessage();
31       redirectUrl_ = rpcError.getRedirectUrl();
32 
33       RpcUnderlyingError rpcErrorCause = rpcError.getError();
34       if (rpcErrorCause != null)
35       {
36          cause_ = new ServerErrorCause(rpcErrorCause.getCode(),
37                                        rpcErrorCause.getCategory(),
38                                        rpcErrorCause.getMessage());
39       }
40       else
41       {
42          cause_ = null;
43       }
44       clientInfo_ = rpcError.getClientInfo();
45    }
46 
47    @Override
toString()48    public String toString()
49    {
50       StringBuffer sb = new StringBuffer();
51       sb.append(code_ + ": " + message_ + "\n");
52       if (cause_ != null)
53          sb.append(cause_.toString());
54       return sb.toString();
55    }
56 
getCode()57    public int getCode()
58    {
59       return code_;
60    }
61 
getMessage()62    public String getMessage()
63    {
64       return message_;
65    }
66 
getRedirectUrl()67    public String getRedirectUrl()
68    {
69       return redirectUrl_;
70    }
71 
getCause()72    public ServerErrorCause getCause()
73    {
74       return cause_;
75    }
76 
getUserMessage()77    public String getUserMessage()
78    {
79       if (cause_ != null)
80          return cause_.getMessage();
81       else
82          return message_;
83    }
84 
85    @Override
getClientInfo()86    public JSONValue getClientInfo()
87    {
88       return clientInfo_ == null ? JSONNull.getInstance() : clientInfo_;
89    }
90 
codeFromRpcErrorCode(int code)91    private int codeFromRpcErrorCode(int code)
92    {
93       switch(code)
94       {
95       case RpcError.SUCCESS:
96          return ServerError.SUCCESS;
97 
98       case RpcError.CONNECTION_ERROR:
99          return ServerError.CONNECTION;
100 
101       case RpcError.UNAVAILABLE:
102          return ServerError.UNAVAILABLE;
103 
104       case RpcError.UNAUTHORIZED:
105          return ServerError.UNAUTHORIZED;
106 
107       case RpcError.PARSE_ERROR:
108       case RpcError.INVALID_REQUEST:
109       case RpcError.METHOD_NOT_FOUND:
110       case RpcError.PARAM_MISSING:
111       case RpcError.PARAM_TYPE_MISMATCH:
112       case RpcError.PARAM_INVALID:
113       case RpcError.METHOD_UNEXEPECTED:
114          return ServerError.PROTOCOL;
115 
116       case RpcError.EXECUTION_ERROR:
117          return ServerError.EXECUTION;
118 
119       case RpcError.TRANSMISSION_ERROR:
120          return ServerError.TRANSMISSION;
121 
122       case RpcError.MAX_SESSIONS_REACHED:
123       case RpcError.MAX_USERS_REACHED:
124          return ServerError.LICENSE_USAGE_LIMIT;
125 
126       default:
127          return ServerError.SUCCESS;
128       }
129    }
130 
131    private int code_;
132    private String message_;
133    private String redirectUrl_;
134    private ServerErrorCause cause_;
135    private JSONValue clientInfo_;
136 }
137