Skip to content

Instantly share code, notes, and snippets.

@spmallette
Created March 24, 2011 14:40
Show Gist options
  • Save spmallette/885165 to your computer and use it in GitHub Desktop.
Save spmallette/885165 to your computer and use it in GitHub Desktop.
post vertex exception handling
@POST
@Path("/{id}")
public Response postVertex(@PathParam("graphname") String graphName, @PathParam("id") String id) {
Graph graph = this.getRexsterApplicationGraph(graphName).getGraph();
Vertex vertex = null;
try {
vertex = graph.getVertex(id);
} catch (RuntimeException re)
{
// not sure i like this assumption: everything's ok...in the case of DEX, it could just
// mean i did not find the vertex i was looking for.
}
try {
if (null == vertex) {
vertex = graph.addVertex(id);
} else {
if (!this.hasElementProperties(this.getRequestObject())) {
JSONObject error = generateErrorObjectJsonFail(new Exception("Vertex with id " + id + " already exists"));
throw new WebApplicationException(this.addHeaders(Response.status(Status.CONFLICT).entity(error)).build());
}
}
Iterator keys = this.getRequestObject().keys();
while (keys.hasNext()) {
String key = keys.next().toString();
if (!key.startsWith(Tokens.UNDERSCORE)) {
vertex.setProperty(key, this.getTypedPropertyValue(this.getRequestObject().getString(key)));
}
}
this.resultObject.put(Tokens.RESULTS, new ElementJSONObject(vertex, this.getReturnKeys(), this.hasShowTypes()));
this.resultObject.put(Tokens.QUERY_TIME, sh.stopWatch());
} catch (JSONException ex) {
logger.error(ex);
JSONObject error = generateErrorObjectJsonFail(ex);
throw new WebApplicationException(this.addHeaders(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error)).build());
} catch (RuntimeException re) {
logger.error(re);
JSONObject error = generateErrorObject(re.getMessage(), re);
throw new WebApplicationException(this.addHeaders(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error)).build());
}
return this.addHeaders(Response.ok(this.resultObject)).build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment