Skip to content

Commit 6ad88e6

Browse files
author
Max Shaposhnik
committed
Merge pull request eclipse-che#307 from eclipse/errorCodes
Add error codes and attributes to server exceptions
2 parents 6bb0aa7 + f9c71ca commit 6ad88e6

12 files changed

Lines changed: 225 additions & 32 deletions

File tree

core/che-core-git-impl-native/src/main/java/org/eclipse/che/git/impl/nativegit/ssh/SshKeyProviderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public byte[] getPrivateKey(String url) throws GitException {
5959
try {
6060
pair = sshService.getPair("git", host);
6161
} catch (ServerException | NotFoundException e) {
62-
throw new GitException("Unable get private ssh key");
62+
throw new GitException("Unable get private ssh key", 32068);
6363
}
6464

6565
// check keys existence
6666
String privateKey = pair.getPrivateKey();
6767
if (privateKey == null) {
68-
throw new GitException("Unable get private ssh key");
68+
throw new GitException("Unable get private ssh key", 32068);
6969
}
7070

7171
final String publicKey = pair.getPublicKey();

core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/commons/exception/ServerException.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
package org.eclipse.che.ide.commons.exception;
1212

1313
import org.eclipse.che.ide.rest.HTTPHeader;
14+
15+
import com.google.gwt.core.client.JavaScriptObject;
1416
import com.google.gwt.http.client.Response;
1517

18+
import java.util.HashMap;
19+
import java.util.Map;
20+
1621
/**
1722
* @author Vitaliy Gulyy
1823
*/
@@ -24,12 +29,18 @@ public class ServerException extends Exception {
2429

2530
private String message = "";
2631

32+
private int errorCode;
33+
34+
private Map<String, String> attributes = new HashMap<>();
35+
2736
private boolean errorMessageProvided;
2837

2938
public ServerException(Response response) {
3039
this.response = response;
3140
this.errorMessageProvided = checkErrorMessageProvided();
3241
this.message = getMessageFromJSON(response.getText());
42+
this.errorCode = getErrorCodeFromJSON(response.getText());
43+
// parseJsonAttributes(response.getText());
3344
}
3445

3546
public ServerException(Response response, String message) {
@@ -57,6 +68,15 @@ public String getMessage() {
5768
return response.getText();
5869
}
5970

71+
public int getErrorCode() {
72+
return errorCode;
73+
}
74+
75+
public Map<String, String> getAttributes() {
76+
return attributes;
77+
}
78+
79+
6080
@Override
6181
public String toString() {
6282
return getMessage();
@@ -70,6 +90,18 @@ private native String getMessageFromJSON(String json) /*-{
7090
}
7191
}-*/;
7292

93+
94+
private native int getErrorCodeFromJSON(String json) /*-{
95+
try {
96+
var result = JSON.parse(json).errorCode;
97+
if (result) {
98+
return result;
99+
}
100+
} catch (e) {
101+
}
102+
return -1;
103+
}-*/;
104+
73105
public String getHeader(String key) {
74106
return response.getHeader(key);
75107
}
@@ -83,6 +115,18 @@ private boolean checkErrorMessageProvided() {
83115
return false;
84116
}
85117

118+
// private native void parseJsonAttributes(String json) /*-{
119+
// try {
120+
// var attributes = JSON.parse(json).attributes;
121+
// for(var key in attributes) {
122+
// this.@org.eclipse.che.ide.commons.exception.ServerException.attributes::put(Ljava/lang/String;Ljava/lang/String;)(key, attributes[key]);
123+
// }
124+
//
125+
// } catch (e) {
126+
// console.log(e.message, e);
127+
// }
128+
// }-*/;
129+
86130
public boolean isErrorMessageProvided() {
87131
return errorMessageProvided;
88132
}

core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/commons/exception/UnauthorizedException.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,15 @@
2020
* @author Sergii Leschenko
2121
*/
2222
@SuppressWarnings("serial")
23-
public class UnauthorizedException extends Exception {
24-
25-
private Response response;
23+
public class UnauthorizedException extends ServerException {
2624

2725
private AsyncRequest request;
2826

2927
public UnauthorizedException(Response response, AsyncRequest request) {
30-
super(response.getText());
31-
this.response = response;
28+
super(response);
3229
this.request = request;
3330
}
3431

35-
public Response getResponse() {
36-
return response;
37-
}
38-
3932
public AsyncRequest getRequest() {
4033
return request;
4134
}

core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/json/JsonHelper.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.gwt.json.client.JSONValue;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
@@ -77,6 +78,8 @@ public static Map<String, List<String>> toMapOfLists(String jsonStr) {
7778
return map;
7879
}
7980

81+
//TODO: find a way to avoid those util methods here.
82+
8083
/** Returns message or result of it parse if the message is json. */
8184
public static String parseJsonMessage(String parsedMessage) {
8285
try {
@@ -89,4 +92,28 @@ public static String parseJsonMessage(String parsedMessage) {
8992
}
9093
}
9194

92-
}
95+
/** Returns message or result of it parse if the message is json. */
96+
public static Map<String, String> parseErrorAttributes(String parsedMessage) {
97+
try {
98+
//parsed message
99+
JSONValue message = JSONParser.parseStrict(parsedMessage).isObject().get("attributes");
100+
return toMap(message.isObject().toString());
101+
} catch (Exception e) {
102+
//not found json in message
103+
return Collections.emptyMap();
104+
}
105+
}
106+
107+
/** Returns message or result of it parse if the message is json. */
108+
public static int parseErrorCode(String parsedMessage) {
109+
try {
110+
//parsed message
111+
JSONValue message = JSONParser.parseStrict(parsedMessage).isObject().get("errorCode");
112+
return new Double(message.isNumber().doubleValue()).intValue();
113+
} catch (Exception e) {
114+
//not found json in message
115+
return -1;
116+
}
117+
}
118+
119+
}

core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/websocket/rest/exceptions/ServerException.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
*******************************************************************************/
1111
package org.eclipse.che.ide.websocket.rest.exceptions;
1212

13+
import org.eclipse.che.ide.json.JsonHelper;
1314
import org.eclipse.che.ide.rest.HTTPHeader;
1415
import org.eclipse.che.ide.websocket.Message;
1516
import org.eclipse.che.ide.websocket.rest.Pair;
1617

1718
import java.util.List;
19+
import java.util.Map;
1820

1921
/**
2022
* Thrown when there was an any exception was received from the server over WebSocket.
@@ -23,20 +25,29 @@
2325
*/
2426
@SuppressWarnings("serial")
2527
public class ServerException extends Exception {
28+
2629
private Message response;
2730

31+
private String message;
32+
33+
private int errorCode;
34+
35+
36+
private Map<String, String> attributes;
37+
2838
private boolean errorMessageProvided;
2939

3040
public ServerException(Message response) {
3141
this.response = response;
42+
this.message = JsonHelper.parseJsonMessage(response.getBody());
43+
this.errorCode = JsonHelper.parseErrorCode(response.getBody());
3244
this.errorMessageProvided = checkErrorMessageProvided();
45+
this.attributes = JsonHelper.parseErrorAttributes(response.getBody());
3346
}
3447

3548
@Override
3649
public String getMessage() {
37-
if (response.getBody() == null || response.getBody().isEmpty())
38-
return null;
39-
return response.getBody();
50+
return message;
4051
}
4152

4253
public int getHTTPStatus() {
@@ -67,4 +78,13 @@ private boolean checkErrorMessageProvided() {
6778
public boolean isErrorMessageProvided() {
6879
return errorMessageProvided;
6980
}
81+
82+
public int getErrorCode() {
83+
return errorCode;
84+
}
85+
86+
public Map<String, String> getAttributes() {
87+
return attributes;
88+
}
89+
7090
}

core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/websocket/rest/exceptions/UnauthorizedException.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818
* @author Artem Zatsarynnyi
1919
*/
2020
@SuppressWarnings("serial")
21-
public class UnauthorizedException extends Exception {
22-
private Message message;
21+
public class UnauthorizedException extends ServerException {
2322

2423
public UnauthorizedException(Message message) {
25-
this.message = message;
24+
super(message);
2625
}
2726

28-
public int getHTTPStatus() {
29-
return message.getResponseCode();
30-
}
27+
3128
}

core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/projectimport/ErrorMessageUtils.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212

1313
import org.eclipse.che.api.core.rest.shared.dto.ServiceError;
1414
import org.eclipse.che.ide.commons.exception.JobNotFoundException;
15+
import org.eclipse.che.ide.commons.exception.ServerException;
1516
import org.eclipse.che.ide.commons.exception.UnauthorizedException;
1617
import org.eclipse.che.ide.dto.DtoFactory;
1718

19+
import java.util.Collections;
20+
import java.util.Map;
21+
1822
/**
1923
* The class contains business logic which allows define type of error.
2024
*
@@ -39,12 +43,43 @@ public static String getErrorMessage(Throwable exception) {
3943
if (exception instanceof JobNotFoundException) {
4044
return "Project import failed";
4145
} else if (exception instanceof UnauthorizedException) {
42-
UnauthorizedException unauthorizedException = (UnauthorizedException)exception;
43-
ServiceError serverError = dtoFactory.createDtoFromJson(unauthorizedException.getResponse().getText(),
44-
ServiceError.class);
45-
return serverError.getMessage();
46+
return ((UnauthorizedException)exception).getMessage();
4647
} else {
4748
return dtoFactory.createDtoFromJson(exception.getMessage(), ServiceError.class).getMessage();
4849
}
4950
}
51+
52+
/**
53+
* Returns error code of the exception if it is of type {@clink ServerException} and has error code set, or -1 otherwise.
54+
*
55+
* @param exception
56+
* passed exception
57+
* @return error code
58+
*/
59+
public static int getErrorCode(Throwable exception) {
60+
if (exception instanceof ServerException) {
61+
return ((ServerException)exception).getErrorCode();
62+
} else if (exception instanceof org.eclipse.che.ide.websocket.rest.exceptions.ServerException) {
63+
return ((org.eclipse.che.ide.websocket.rest.exceptions.ServerException)exception).getErrorCode();
64+
} else {
65+
return -1;
66+
}
67+
}
68+
69+
/**
70+
* Returns attributes of the exception if it is of type {@clink ServerException} and has attributes set, or empty map otherwise.
71+
*
72+
* @param exception
73+
* passed exception
74+
* @return error code
75+
*/
76+
public static Map<String, String> getAttributes(Throwable exception) {
77+
if (exception instanceof ServerException) {
78+
return ((ServerException)exception).getAttributes();
79+
} else if (exception instanceof org.eclipse.che.ide.websocket.rest.exceptions.ServerException) {
80+
return ((org.eclipse.che.ide.websocket.rest.exceptions.ServerException)exception).getAttributes();
81+
} else {
82+
return Collections.emptyMap();
83+
}
84+
}
5085
}

core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/projectimport/wizard/ProjectImporter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.eclipse.che.ide.api.wizard.Wizard.CompleteCallback;
3131
import org.eclipse.che.ide.projectimport.ErrorMessageUtils;
3232
import org.eclipse.che.ide.rest.AsyncRequestCallback;
33+
import org.eclipse.che.ide.util.loging.Log;
3334

3435
import javax.validation.constraints.NotNull;
3536

@@ -99,12 +100,13 @@ public void apply(Void arg) throws OperationException {
99100
@Override
100101
public void apply(PromiseError exception) throws OperationException {
101102
subscriber.onFailure(exception.getMessage());
102-
String errorMessage = ErrorMessageUtils.getErrorMessage(exception.getCause());
103-
if (errorMessage.equals("Unable get private ssh key")) {
103+
int errorCode = ErrorMessageUtils.getErrorCode(exception.getCause());
104+
// no ssh key found code. See org.eclipse.che.git.impl.nativegit.ssh.SshKeyProviderImpl.
105+
if (errorCode == 32068) {
104106
callback.onFailure(new Exception(localizationConstant.importProjectMessageUnableGetSshKey()));
105107
return;
106108
}
107-
callback.onFailure(new Exception(errorMessage));
109+
callback.onFailure(new Exception(exception.getCause().getMessage()));
108110
}
109111
});
110112
}

core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/projectimport/wizard/ProjectResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ protected void onSuccess(List<SourceEstimation> result) {
127127
@Override
128128
protected void onFailure(Throwable exception) {
129129
projectNotificationSubscriber.onFailure(exception.getMessage());
130-
String errorMessage = ErrorMessageUtils.getErrorMessage(exception);
131-
callback.onFailure(new Exception(errorMessage));
130+
callback.onFailure(new Exception(exception.getMessage()));
132131
}
133132
});
134133
}

core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/projectimport/wizard/ProjectUpdater.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ protected void onSuccess(final ProjectConfigDto result) {
8989
@Override
9090
protected void onFailure(Throwable exception) {
9191
projectNotificationSubscriber.onFailure(exception.getMessage());
92-
String errorMessage = ErrorMessageUtils.getErrorMessage(exception);
93-
callback.onFailure(new Exception(errorMessage));
92+
callback.onFailure(new Exception(exception.getMessage()));
9493
}
9594
});
9695
}

0 commit comments

Comments
 (0)