Commit 6de1b845 by Venkatesh Seetharam

BUG-38869 update entity for an entity that does not exist returns a 400 instead of 404

BUG-38726 add trait to an entity that does not exist returns a 400, it should return a 404
parent e688ca0f
...@@ -23,7 +23,7 @@ import org.apache.hadoop.metadata.MetadataException; ...@@ -23,7 +23,7 @@ import org.apache.hadoop.metadata.MetadataException;
/** /**
* A simple wrapper for 404. * A simple wrapper for 404.
*/ */
public class EntityNotFoundException extends MetadataException { public class EntityNotFoundException extends RepositoryException {
public EntityNotFoundException() { public EntityNotFoundException() {
} }
......
...@@ -162,9 +162,9 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -162,9 +162,9 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
public ITypedReferenceableInstance getEntityDefinition(String guid) throws RepositoryException { public ITypedReferenceableInstance getEntityDefinition(String guid) throws RepositoryException {
LOG.info("Retrieving entity with guid={}", guid); LOG.info("Retrieving entity with guid={}", guid);
try { Vertex instanceVertex = getVertexForGUID(guid);
Vertex instanceVertex = getVertexForGUID(guid);
try {
LOG.debug("Found a vertex {} for guid {}", instanceVertex, guid); LOG.debug("Found a vertex {} for guid {}", instanceVertex, guid);
return graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertex); return graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertex);
......
...@@ -21,6 +21,8 @@ package org.apache.hadoop.metadata.web.resources; ...@@ -21,6 +21,8 @@ package org.apache.hadoop.metadata.web.resources;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.apache.hadoop.metadata.MetadataException; import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.MetadataServiceClient; import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.ParamChecker;
import org.apache.hadoop.metadata.repository.EntityNotFoundException;
import org.apache.hadoop.metadata.services.MetadataService; import org.apache.hadoop.metadata.services.MetadataService;
import org.apache.hadoop.metadata.typesystem.types.ValueConversionException; import org.apache.hadoop.metadata.typesystem.types.ValueConversionException;
import org.apache.hadoop.metadata.web.util.Servlets; import org.apache.hadoop.metadata.web.util.Servlets;
...@@ -43,7 +45,6 @@ import javax.ws.rs.Produces; ...@@ -43,7 +45,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
...@@ -131,6 +132,7 @@ public class EntityResource { ...@@ -131,6 +132,7 @@ public class EntityResource {
public Response getEntityDefinition(@PathParam("guid") String guid) { public Response getEntityDefinition(@PathParam("guid") String guid) {
try { try {
LOG.debug("Fetching entity definition for guid={} ", guid); LOG.debug("Fetching entity definition for guid={} ", guid);
ParamChecker.notEmpty(guid, "guid cannot be null");
final String entityDefinition = metadataService.getEntityDefinition(guid); final String entityDefinition = metadataService.getEntityDefinition(guid);
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
...@@ -148,10 +150,14 @@ public class EntityResource { ...@@ -148,10 +150,14 @@ public class EntityResource {
return Response.status(status).entity(response).build(); return Response.status(status).entity(response).build();
} catch (MetadataException | IllegalArgumentException e) { } catch (EntityNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e); LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND)); Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (MetadataException | IllegalArgumentException e) {
LOG.error("Bad GUID={}", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (Throwable e) { } catch (Throwable e) {
LOG.error("Unable to get instance definition for GUID {}", guid, e); LOG.error("Unable to get instance definition for GUID {}", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
...@@ -218,6 +224,10 @@ public class EntityResource { ...@@ -218,6 +224,10 @@ public class EntityResource {
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Thread.currentThread().getName()); response.put(MetadataServiceClient.REQUEST_ID, Thread.currentThread().getName());
return Response.ok(response).build(); return Response.ok(response).build();
} catch (EntityNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (MetadataException | IllegalArgumentException e) { } catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to add property {} to entity id {}", property, guid, e); LOG.error("Unable to add property {} to entity id {}", property, guid, e);
throw new WebApplicationException( throw new WebApplicationException(
...@@ -251,6 +261,10 @@ public class EntityResource { ...@@ -251,6 +261,10 @@ public class EntityResource {
response.put(MetadataServiceClient.COUNT, traitNames.size()); response.put(MetadataServiceClient.COUNT, traitNames.size());
return Response.ok(response).build(); return Response.ok(response).build();
} catch (EntityNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (MetadataException | IllegalArgumentException e) { } catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to get trait names for entity {}", guid, e); LOG.error("Unable to get trait names for entity {}", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
...@@ -286,6 +300,10 @@ public class EntityResource { ...@@ -286,6 +300,10 @@ public class EntityResource {
response.put(MetadataServiceClient.GUID, guid); response.put(MetadataServiceClient.GUID, guid);
return Response.created(locationURI).entity(response).build(); return Response.created(locationURI).entity(response).build();
} catch (EntityNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (MetadataException | IOException | IllegalArgumentException e) { } catch (MetadataException | IOException | IllegalArgumentException e) {
LOG.error("Unable to add trait for entity={}", guid, e); LOG.error("Unable to add trait for entity={}", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
...@@ -320,6 +338,10 @@ public class EntityResource { ...@@ -320,6 +338,10 @@ public class EntityResource {
response.put(TRAIT_NAME, traitName); response.put(TRAIT_NAME, traitName);
return Response.ok(response).build(); return Response.ok(response).build();
} catch (EntityNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (MetadataException | IllegalArgumentException e) { } catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to delete trait name={} for entity={}", traitName, guid, e); LOG.error("Unable to delete trait name={} for entity={}", traitName, guid, e);
throw new WebApplicationException( throw new WebApplicationException(
......
...@@ -33,7 +33,6 @@ import javax.inject.Singleton; ...@@ -33,7 +33,6 @@ import javax.inject.Singleton;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/** /**
......
...@@ -38,7 +38,6 @@ import javax.ws.rs.Path; ...@@ -38,7 +38,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -320,4 +319,4 @@ public class MetadataDiscoveryResource { ...@@ -320,4 +319,4 @@ public class MetadataDiscoveryResource {
return response; return response;
} }
} }
} }
\ No newline at end of file
...@@ -46,7 +46,6 @@ import javax.ws.rs.PathParam; ...@@ -46,7 +46,6 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
......
...@@ -43,7 +43,6 @@ import javax.ws.rs.Produces; ...@@ -43,7 +43,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
......
...@@ -389,7 +389,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -389,7 +389,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
@Test(dependsOnMethods = "testAddTrait") @Test(dependsOnMethods = "testAddTrait")
public void testAddExistingTrait() throws Exception { public void testAddExistingTrait() throws Exception {
final String traitName = "PII_Trait"; final String traitName = "PII_Trait" + randomString();
Struct traitInstance = new Struct(traitName); Struct traitInstance = new Struct(traitName);
String traitInstanceAsJSON = InstanceSerialization.toJson(traitInstance, true); String traitInstanceAsJSON = InstanceSerialization.toJson(traitInstance, true);
...@@ -408,7 +408,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -408,7 +408,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
@Test(dependsOnMethods = "testGetTraitNames") @Test(dependsOnMethods = "testGetTraitNames")
public void testAddTraitWithAttribute() throws Exception { public void testAddTraitWithAttribute() throws Exception {
final String traitName = "P_I_I"; final String traitName = "PII_Trait" + randomString();
HierarchicalTypeDefinition<TraitType> piiTrait = HierarchicalTypeDefinition<TraitType> piiTrait =
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of(), TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of(),
TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE)); TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE));
...@@ -456,7 +456,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -456,7 +456,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
@Test @Test
public void testAddTraitWithNoRegistration() throws Exception { public void testAddTraitWithNoRegistration() throws Exception {
final String traitName = "PII_Trait_Blah"; final String traitName = "PII_Trait" + randomString();
HierarchicalTypeDefinition<TraitType> piiTrait = HierarchicalTypeDefinition<TraitType> piiTrait =
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of()); TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true); String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment