Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
4c028c4f
Commit
4c028c4f
authored
Oct 27, 2018
by
Greg Messner
Browse files
Mods to support error, message, and validation errors (#260).
parent
8176577f
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/GitLabApiException.java
View file @
4c028c4f
package
org.gitlab4j.api
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response.StatusType
;
import
org.gitlab4j.api.models.ErrorMessage
;
import
org.gitlab4j.api.utils.JacksonJson
;
import
com.fasterxml.jackson.databind.JsonNode
;
/**
* This is the exception that will be thrown if any exception occurs while communicating
...
...
@@ -15,6 +25,7 @@ public class GitLabApiException extends Exception {
private
StatusType
statusInfo
;
private
int
httpStatus
;
private
String
message
;
private
Map
<
String
,
List
<
String
>>
validationErrors
;
/**
* Create a GitLabApiException instance with the specified message.
...
...
@@ -28,7 +39,7 @@ public class GitLabApiException extends Exception {
/**
* Create a GitLabApiException instance based on the ClientResponse.
*
*
* @param response the JAX-RS response that caused the exception
*/
public
GitLabApiException
(
Response
response
)
{
...
...
@@ -38,10 +49,50 @@ public class GitLabApiException extends Exception {
httpStatus
=
response
.
getStatus
();
if
(
response
.
hasEntity
())
{
try
{
ErrorMessage
errorMessage
=
response
.
readEntity
(
ErrorMessage
.
class
);
message
=
errorMessage
.
getMessage
();
String
message
=
response
.
readEntity
(
String
.
class
);
this
.
message
=
message
;
// Determine what is in the content of the response and process it accordingly
MediaType
mediaType
=
response
.
getMediaType
();
if
(
mediaType
!=
null
&&
"json"
.
equals
(
mediaType
.
getSubtype
()))
{
JsonNode
json
=
JacksonJson
.
toJsonNode
(
message
);
// First see if it is a "message", if so it is either a simple message,
// or a Map<String, List<String>> of validation errors
JsonNode
jsonMessage
=
json
.
get
(
"message"
);
if
(
jsonMessage
!=
null
)
{
// If the node is an object, then it is validation errors
if
(
jsonMessage
.
isObject
())
{
validationErrors
=
new
HashMap
<>();
Iterator
<
Entry
<
String
,
JsonNode
>>
fields
=
jsonMessage
.
fields
();
while
(
fields
.
hasNext
())
{
Entry
<
String
,
JsonNode
>
field
=
fields
.
next
();
List
<
String
>
values
=
new
ArrayList
<>();
validationErrors
.
put
(
field
.
getKey
(),
values
);
for
(
JsonNode
value
:
field
.
getValue
())
{
values
.
add
(
value
.
asText
());
}
}
}
else
{
this
.
message
=
jsonMessage
.
asText
();
}
}
else
{
JsonNode
jsonError
=
json
.
get
(
"error"
);
if
(
jsonError
!=
null
)
{
this
.
message
=
jsonError
.
asText
();
}
}
}
}
catch
(
Exception
ignore
)
{
}
...
...
@@ -50,7 +101,7 @@ public class GitLabApiException extends Exception {
/**
* Create a GitLabApiException instance based on the exception.
*
*
* @param e the Exception to wrap
*/
public
GitLabApiException
(
Exception
e
)
{
...
...
@@ -60,7 +111,7 @@ public class GitLabApiException extends Exception {
/**
* Get the message associated with the exception.
*
*
* @return the message associated with the exception
*/
@Override
...
...
@@ -71,7 +122,7 @@ public class GitLabApiException extends Exception {
/**
* Returns the HTTP status reason message, returns null if the
* causing error was not an HTTP related exception.
*
*
* @return the HTTP status reason message
*/
public
final
String
getReason
()
{
...
...
@@ -81,10 +132,32 @@ public class GitLabApiException extends Exception {
/**
* Returns the HTTP status code that was the cause of the exception. returns 0 if the
* causing error was not an HTTP related exception.
*
*
* @return the HTTP status code, returns 0 if the causing error was not an HTTP related exception
*/
public
final
int
getHttpStatus
()
{
return
(
httpStatus
);
}
/**
* Returns true if this GitLabApiException was caused by validation errors on the GitLab server,
* otherwise returns false.
*
* @return true if this GitLabApiException was caused by validation errors on the GitLab server,
* otherwise returns false
*/
public
boolean
hasValidationErrors
()
{
return
(
validationErrors
!=
null
);
}
/**
* Returns a Map<String, List<String>> instance containing validation errors if this GitLabApiException
* was caused by validation errors on the GitLab server, otherwise returns null.
*
* @return a Map<String, List<String>> instance containing validation errors if this GitLabApiException
* was caused by validation errors on the GitLab server, otherwise returns null
*/
public
Map
<
String
,
List
<
String
>>
getValidationErrors
()
{
return
(
validationErrors
);
}
}
src/main/java/org/gitlab4j/api/utils/JacksonJson.java
View file @
4c028c4f
...
...
@@ -243,4 +243,15 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
public
static
<
T
>
String
toJsonString
(
final
T
object
)
{
return
(
JacksonJsonSingletonHelper
.
JACKSON_JSON
.
marshal
(
object
));
}
/**
* Parse the provided String into a JsonNode instance.
*
* @param jsonString a String containing JSON to parse
* @return a JsonNode with the String parsed into a JSON tree
* @throws IOException if any IO error occurs
*/
public
static
JsonNode
toJsonNode
(
String
json
)
throws
IOException
{
return
(
JacksonJsonSingletonHelper
.
JACKSON_JSON
.
objectMapper
.
readTree
(
json
));
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment