1
1
package org .gitlab4j .api ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .HashMap ;
5
+ import java .util .Iterator ;
6
+ import java .util .List ;
7
+ import java .util .Map ;
8
+ import java .util .Map .Entry ;
9
+
10
+ import javax .ws .rs .core .MediaType ;
3
11
import javax .ws .rs .core .Response ;
4
12
import javax .ws .rs .core .Response .StatusType ;
5
13
6
- import org .gitlab4j .api .models .ErrorMessage ;
14
+ import org .gitlab4j .api .utils .JacksonJson ;
15
+
16
+ import com .fasterxml .jackson .databind .JsonNode ;
7
17
8
18
/**
9
19
* This is the exception that will be thrown if any exception occurs while communicating
@@ -15,6 +25,7 @@ public class GitLabApiException extends Exception {
15
25
private StatusType statusInfo ;
16
26
private int httpStatus ;
17
27
private String message ;
28
+ private Map <String , List <String >> validationErrors ;
18
29
19
30
/**
20
31
* Create a GitLabApiException instance with the specified message.
@@ -28,7 +39,7 @@ public GitLabApiException(String message) {
28
39
29
40
/**
30
41
* Create a GitLabApiException instance based on the ClientResponse.
31
- *
42
+ *
32
43
* @param response the JAX-RS response that caused the exception
33
44
*/
34
45
public GitLabApiException (Response response ) {
@@ -38,10 +49,50 @@ public GitLabApiException(Response response) {
38
49
httpStatus = response .getStatus ();
39
50
40
51
if (response .hasEntity ()) {
52
+
41
53
try {
42
54
43
- ErrorMessage errorMessage = response .readEntity (ErrorMessage .class );
44
- message = errorMessage .getMessage ();
55
+ String message = response .readEntity (String .class );
56
+ this .message = message ;
57
+
58
+ // Determine what is in the content of the response and process it accordingly
59
+ MediaType mediaType = response .getMediaType ();
60
+ if (mediaType != null && "json" .equals (mediaType .getSubtype ())) {
61
+
62
+ JsonNode json = JacksonJson .toJsonNode (message );
63
+
64
+ // First see if it is a "message", if so it is either a simple message,
65
+ // or a Map<String, List<String>> of validation errors
66
+ JsonNode jsonMessage = json .get ("message" );
67
+ if (jsonMessage != null ) {
68
+
69
+ // If the node is an object, then it is validation errors
70
+ if (jsonMessage .isObject ()) {
71
+
72
+ validationErrors = new HashMap <>();
73
+ Iterator <Entry <String , JsonNode >> fields = jsonMessage .fields ();
74
+ while (fields .hasNext ()) {
75
+
76
+ Entry <String , JsonNode > field = fields .next ();
77
+ List <String > values = new ArrayList <>();
78
+ validationErrors .put (field .getKey (), values );
79
+ for (JsonNode value : field .getValue ()) {
80
+ values .add (value .asText ());
81
+ }
82
+ }
83
+
84
+ } else {
85
+ this .message = jsonMessage .asText ();
86
+ }
87
+
88
+ } else {
89
+
90
+ JsonNode jsonError = json .get ("error" );
91
+ if (jsonError != null ) {
92
+ this .message = jsonError .asText ();
93
+ }
94
+ }
95
+ }
45
96
46
97
} catch (Exception ignore ) {
47
98
}
@@ -50,7 +101,7 @@ public GitLabApiException(Response response) {
50
101
51
102
/**
52
103
* Create a GitLabApiException instance based on the exception.
53
- *
104
+ *
54
105
* @param e the Exception to wrap
55
106
*/
56
107
public GitLabApiException (Exception e ) {
@@ -60,7 +111,7 @@ public GitLabApiException(Exception e) {
60
111
61
112
/**
62
113
* Get the message associated with the exception.
63
- *
114
+ *
64
115
* @return the message associated with the exception
65
116
*/
66
117
@ Override
@@ -71,7 +122,7 @@ public final String getMessage() {
71
122
/**
72
123
* Returns the HTTP status reason message, returns null if the
73
124
* causing error was not an HTTP related exception.
74
- *
125
+ *
75
126
* @return the HTTP status reason message
76
127
*/
77
128
public final String getReason () {
@@ -81,10 +132,32 @@ public final String getReason() {
81
132
/**
82
133
* Returns the HTTP status code that was the cause of the exception. returns 0 if the
83
134
* causing error was not an HTTP related exception.
84
- *
135
+ *
85
136
* @return the HTTP status code, returns 0 if the causing error was not an HTTP related exception
86
137
*/
87
138
public final int getHttpStatus () {
88
139
return (httpStatus );
89
140
}
141
+
142
+ /**
143
+ * Returns true if this GitLabApiException was caused by validation errors on the GitLab server,
144
+ * otherwise returns false.
145
+ *
146
+ * @return true if this GitLabApiException was caused by validation errors on the GitLab server,
147
+ * otherwise returns false
148
+ */
149
+ public boolean hasValidationErrors () {
150
+ return (validationErrors != null );
151
+ }
152
+
153
+ /**
154
+ * Returns a Map<String, List<String>> instance containing validation errors if this GitLabApiException
155
+ * was caused by validation errors on the GitLab server, otherwise returns null.
156
+ *
157
+ * @return a Map<String, List<String>> instance containing validation errors if this GitLabApiException
158
+ * was caused by validation errors on the GitLab server, otherwise returns null
159
+ */
160
+ public Map <String , List <String >> getValidationErrors () {
161
+ return (validationErrors );
162
+ }
90
163
}
0 commit comments