Index: webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java =================================================================== --- webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java (revision 1418168) +++ webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java (working copy) @@ -48,6 +48,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class TestHCatClient { @@ -403,4 +404,77 @@ assertTrue("Unexpected exception: " + exception.getMessage(), false); } } + + @Test + public void testObjectNotFoundException() throws Exception { + try { + + HCatClient client = HCatClient.create(new Configuration(hcatConf)); + String dbName = "testObjectNotFoundException_DBName"; + String tableName = "testObjectNotFoundException_TableName"; + client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); + + try { // Test that fetching a non-existent db-name yields ObjectNotFound. + client.getDatabase(dbName); + assertTrue("Expected ObjectNotFoundException.", false); + } catch(Exception exception) { + LOG.info("Got exception: ", exception); + assertTrue("Expected ObjectNotFoundException. Got:" + exception.getClass(), + exception instanceof ObjectNotFoundException); + } + + client.createDatabase(HCatCreateDBDesc.create(dbName).build()); + + try { // Test that fetching a non-existent table-name yields ObjectNotFound. + client.getTable(dbName, tableName); + assertTrue("Expected ObjectNotFoundException.", false); + } catch(Exception exception) { + LOG.info("Got exception: ", exception); + assertTrue("Expected ObjectNotFoundException. Got:" + exception.getClass(), + exception instanceof ObjectNotFoundException); + } + + String partitionColumn = "part"; + + List columns = Arrays.asList(new HCatFieldSchema("col", Type.STRING, "")); + ArrayList partitionColumns = new ArrayList( + Arrays.asList(new HCatFieldSchema(partitionColumn, Type.STRING, ""))); + client.createTable(HCatCreateTableDesc.create(dbName, tableName, columns) + .partCols(partitionColumns) + .build()); + + Map partitionSpec = new HashMap(); + partitionSpec.put(partitionColumn, "foobar"); + try { // Test that fetching a non-existent partition yields ObjectNotFound. + client.getPartition(dbName, tableName, partitionSpec); + assertTrue("Expected ObjectNotFoundException.", false); + } catch(Exception exception) { + LOG.info("Got exception: ", exception); + assertTrue("Expected ObjectNotFoundException. Got:" + exception.getClass(), + exception instanceof ObjectNotFoundException); + } + + client.addPartition(HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build()); + + // Test that listPartitionsByFilter() returns an empty-set, if the filter selects no partitions. + assertEquals("Expected empty set of partitions.", + 0, client.listPartitionsByFilter(dbName, tableName, partitionColumn + " < 'foobar'").size()); + + try { // Test that listPartitionsByFilter() throws HCatException if the partition-key is incorrect. + partitionSpec.put("NonExistentKey", "foobar"); + client.getPartition(dbName, tableName, partitionSpec); + assertTrue("Expected HCatException.", false); + } catch(Exception exception) { + LOG.info("Got exception: ", exception); + assertTrue("Expected HCatException. Got:" + exception.getClass(), + exception instanceof HCatException); + assertFalse("Did not expect ObjectNotFoundException.", exception instanceof ObjectNotFoundException); + } + + } + catch (Throwable t) { + LOG.error("Unexpected exception!", t); + assertTrue("Unexpected exception! " + t.getMessage(), false); + } + } } Index: webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java =================================================================== --- webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java (revision 0) +++ webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java (revision 0) @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hcatalog.api; + +import org.apache.hcatalog.common.HCatException; + +/** + * This exception is thrown when a Database, Table or Partition + * specified in an HCatalog query is not found. + */ +public class ObjectNotFoundException extends HCatException { + + private static final long serialVersionUID = 1L; + + /** + * @param message Exception message. + * @param cause The wrapped Throwable that caused this exception. + */ + public ObjectNotFoundException(String message, Throwable cause) { + super(message, cause); + } +} Index: webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java =================================================================== --- webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java (revision 1418168) +++ webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java (working copy) @@ -28,8 +28,8 @@ private static final long serialVersionUID = 1L; /** - * @param message - * @param cause + * @param message Exception message. + * @param cause The wrapped Throwable that caused this exception. */ public ConnectionFailureException(String message, Throwable cause) { super(message, cause); Index: webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java =================================================================== --- webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java (revision 1418168) +++ webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java (working copy) @@ -79,7 +79,7 @@ db = new HCatDatabase(hiveDB); } } catch (NoSuchObjectException exp) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while fetching database", exp); } catch (MetaException exp) { throw new HCatException("MetaException while fetching database", @@ -120,7 +120,7 @@ hmsClient.dropDatabase(checkDB(dbName), true, ifExists, isCascade); } catch (NoSuchObjectException e) { if (!ifExists) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while dropping db.", e); } } catch (InvalidOperationException e) { @@ -162,7 +162,7 @@ throw new ConnectionFailureException( "TException while fetching table.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while fetching table.", e); } return table; @@ -184,7 +184,7 @@ } catch (MetaException e) { throw new HCatException("MetaException while creating table.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while creating table.", e); } catch (TException e) { throw new ConnectionFailureException( @@ -209,7 +209,7 @@ throw new HCatException("MetaException while updating table schema.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while updating table schema.", e); } catch (TException e) { @@ -242,7 +242,7 @@ throw new HCatException( "MetaException in create table like command.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException in create table like command.", e); } catch (TException e) { @@ -259,7 +259,7 @@ hmsClient.dropTable(checkDB(dbName), tableName,true, ifExists); } catch (NoSuchObjectException e) { if (!ifExists) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while dropping table.", e); } } catch (MetaException e) { @@ -294,7 +294,7 @@ throw new ConnectionFailureException( "TException while renaming table", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while renaming table", e); } catch (InvalidOperationException e) { throw new HCatException( @@ -313,7 +313,7 @@ hcatPtns.add(new HCatPartition(ptn)); } } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while retrieving partition.", e); } catch (MetaException e) { throw new HCatException( @@ -344,7 +344,7 @@ throw new ConnectionFailureException( "TException while retrieving partition.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while retrieving partition.", e); } return partition; @@ -376,7 +376,7 @@ throw new ConnectionFailureException( "TException while adding partition.", e); } catch (NoSuchObjectException e) { - throw new HCatException("The table " + partInfo.getTableName() + throw new ObjectNotFoundException("The table " + partInfo.getTableName() + " is could not be found.", e); } } @@ -392,7 +392,7 @@ ifExists); } catch (NoSuchObjectException e) { if (!ifExists) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while dropping partition.", e); } } catch (MetaException e) { @@ -418,7 +418,7 @@ throw new HCatException("MetaException while fetching partitions.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while fetching partitions.", e); } catch (TException e) { throw new ConnectionFailureException( @@ -438,7 +438,7 @@ throw new HCatException( "MetaException while marking partition for event.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while marking partition for event.", e); } catch (UnknownTableException e) { @@ -474,7 +474,7 @@ throw new HCatException( "MetaException while checking partition for event.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while checking partition for event.", e); } catch (UnknownTableException e) { @@ -584,7 +584,7 @@ throw new ConnectionFailureException( "TException while retrieving existing table.", e1); } catch (NoSuchObjectException e1) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while retrieving existing table.", e1); } @@ -666,7 +666,7 @@ throw new ConnectionFailureException( "TException while adding partition.", e); } catch (NoSuchObjectException e) { - throw new HCatException("The table " + throw new ObjectNotFoundException("The table " + partInfoList.get(0).getTableName() + " is could not be found.", e); }