@Provider public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> { public Response toResponse(BadRequestException ex) { return Response.status(404). entity(ex.getMessage()). type("text/plain"). build(); } }
import org.apache.log4j.Logger; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class BadRequestExceptionMapper implements ExceptionMapper { private static final Logger log = Logger.getLogger(BadRequestExceptionMapper.class); @Override public Response toResponse(BadRequestException e) { log.info("Bad request: " + e.getMessage()); return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); } }
@Controller @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) public class SomeController { ... @ExceptionHandler(BadRequestException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public @ResponseBody Map<String,Object> handleIndexNotFoundException(BadRequestException bre, HttpServletRequest request, HttpServletResponse resp) { HashMap<String, Object> result = new HashMap<>(); result.put("error", true); result.put("error_message", bre.getMessage()); return result; } }
@POST @Path("/data/datasets/{name}/admin/drop") public void drop(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String instanceName) throws Exception { propagateUserId(request); InternalDatasetDropParams params = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), InternalDatasetDropParams.class); Preconditions.checkArgument(params.getInstanceSpec() != null, "Missing required 'instanceSpec' parameter."); Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter."); DatasetSpecification spec = params.getInstanceSpec(); DatasetTypeMeta typeMeta = params.getTypeMeta(); try { datasetAdminService.drop(new DatasetId(namespaceId, instanceName), typeMeta, spec); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(spec)); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } }
@POST @Path("/data/datasets/{name}/admin/drop") public void drop(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String instanceName) throws Exception { propagateUserId(request); InternalDatasetDropParams params = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), InternalDatasetDropParams.class); Preconditions.checkArgument(params.getInstanceSpec() != null, "Missing required 'instanceSpec' parameter."); Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter."); DatasetSpecification spec = params.getInstanceSpec(); DatasetTypeMeta typeMeta = params.getTypeMeta(); try { datasetAdminService.drop(new DatasetId(namespaceId, instanceName), typeMeta, spec); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(spec)); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } }
} catch (BadRequestException e) { issuedStops.add(Futures.immediateFuture( new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage())));
} catch (BadRequestException e) { issuedStops.add(Futures.immediateFuture( new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage())));
@POST @Path("/data/datasets/{name}/admin/update") public void update(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) { propagateUserId(request); InternalDatasetUpdateParams params = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), InternalDatasetUpdateParams.class); Preconditions.checkArgument(params.getProperties() != null, "Missing required 'instanceProps' parameter."); Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter."); Preconditions.checkArgument(params.getExistingSpec() != null, "Missing required 'existingSpec' parameter."); DatasetProperties props = params.getProperties(); DatasetSpecification existing = params.getExistingSpec(); DatasetTypeMeta typeMeta = params.getTypeMeta(); try { DatasetId instanceId = new DatasetId(namespaceId, name); DatasetCreationResponse response = datasetAdminService.createOrUpdate(instanceId, typeMeta, props, existing); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(response)); } catch (NotFoundException e) { LOG.debug("Got handler exception", e); responder.sendString(HttpResponseStatus.NOT_FOUND, StringUtils.defaultIfEmpty(e.getMessage(), "")); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (IncompatibleUpdateException e) { responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage()); } catch (Exception e) { responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage()); } }
output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.code(), e.getMessage())); } catch (BadRequestException e) { output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage())); } catch (ConflictException e) { output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.code(), e.getMessage()));
@POST @Path("/data/datasets/{name}/admin/update") public void update(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) { propagateUserId(request); InternalDatasetUpdateParams params = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), InternalDatasetUpdateParams.class); Preconditions.checkArgument(params.getProperties() != null, "Missing required 'instanceProps' parameter."); Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter."); Preconditions.checkArgument(params.getExistingSpec() != null, "Missing required 'existingSpec' parameter."); DatasetProperties props = params.getProperties(); DatasetSpecification existing = params.getExistingSpec(); DatasetTypeMeta typeMeta = params.getTypeMeta(); try { DatasetId instanceId = new DatasetId(namespaceId, name); DatasetCreationResponse response = datasetAdminService.createOrUpdate(instanceId, typeMeta, props, existing); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(response)); } catch (NotFoundException e) { LOG.debug("Got handler exception", e); responder.sendString(HttpResponseStatus.NOT_FOUND, StringUtils.defaultIfEmpty(e.getMessage(), "")); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (IncompatibleUpdateException e) { responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage()); } catch (Exception e) { responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage()); } }
output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.code(), e.getMessage())); } catch (BadRequestException e) { output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage())); } catch (ConflictException e) { output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.code(), e.getMessage()));
private static void verifyAlreadyExist(NamespaceMeta namespaceMeta, NamespaceId existingNamespace) throws Exception { try { namespaceAdmin.create(namespaceMeta); Assert.fail(String.format("Namespace '%s' should not have been created", namespaceMeta.getName())); } catch (BadRequestException e) { Assert.assertTrue(e.getMessage().contains(existingNamespace.getNamespace())); } }
"Error performing IO while writing artifact."); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (UnauthorizedException e) { responder.sendString(HttpResponseStatus.FORBIDDEN, e.getMessage());
"Error performing IO while writing artifact."); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (UnauthorizedException e) { responder.sendString(HttpResponseStatus.FORBIDDEN, e.getMessage());