Skip to content

Commit

Permalink
update TableNoPermissionException to NoPermissionException for suppor…
Browse files Browse the repository at this point in the history
…ting database
  • Loading branch information
jerry-024 committed Dec 20, 2024
1 parent e18ed60 commit 303fd1e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
45 changes: 23 additions & 22 deletions paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,29 @@ default void repairTable(Identifier identifier) throws TableNotExistException {
throw new UnsupportedOperationException();
}

/**
* Exception for trying to operate on a resource that doesn't have permission. Define as a
* runtime exception: 1. Other engine has no this type exception. 2. It wouldn't bring api break
* change.
*/
class NoPermissionException extends RuntimeException {
private static final String MSG = "No permission for %s %s.";

public static NoPermissionException createDatabaseNoPermissionException(
String databaseName, Throwable cause) {
return new NoPermissionException("database", databaseName, cause);
}

public static NoPermissionException createTableNoPermissionException(
Identifier identifier, Throwable cause) {
return new NoPermissionException("table", identifier.getFullName(), cause);
}

public NoPermissionException(String resourceType, String resourceName, Throwable cause) {
super(String.format(MSG, resourceType, resourceName), cause);
}
}

/** Exception for trying to drop on a database that is not empty. */
class DatabaseNotEmptyException extends Exception {
private static final String MSG = "Database %s is not empty.";
Expand Down Expand Up @@ -475,28 +498,6 @@ public Identifier identifier() {
}
}

/**
* Exception for trying to operate on a table that doesn't have permission. Define as a runtime
* exception: 1. Other engine has no this type exception. 2. It wouldn't bring api break change.
*/
class TableNoPermissionException extends RuntimeException {
private static final String MSG = "No permission for Table %s.";
private final Identifier identifier;

public TableNoPermissionException(Identifier identifier) {
this(identifier, null);
}

public TableNoPermissionException(Identifier identifier, Throwable cause) {
super(String.format(MSG, identifier.getFullName()), cause);
this.identifier = identifier;
}

public Identifier identifier() {
return identifier;
}
}

/** Exception for trying to operate on a partition that doesn't exist. */
class PartitionNotExistException extends Exception {

Expand Down
16 changes: 12 additions & 4 deletions paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public void createDatabase(String name, boolean ignoreIfExists, Map<String, Stri
if (!ignoreIfExists) {
throw new DatabaseAlreadyExistException(name);
}
} catch (ForbiddenException e) {
throw NoPermissionException.createDatabaseNoPermissionException(name, e);
}
}

Expand All @@ -212,6 +214,8 @@ public Database getDatabase(String name) throws DatabaseNotExistException {
name, response.options(), response.comment().orElseGet(() -> null));
} catch (NoSuchResourceException e) {
throw new DatabaseNotExistException(name);
} catch (ForbiddenException e) {
throw NoPermissionException.createDatabaseNoPermissionException(name, e);
}
}

Expand All @@ -228,6 +232,8 @@ public void dropDatabase(String name, boolean ignoreIfNotExists, boolean cascade
if (!ignoreIfNotExists) {
throw new DatabaseNotExistException(name);
}
} catch (ForbiddenException e) {
throw NoPermissionException.createDatabaseNoPermissionException(name, e);
}
}

Expand Down Expand Up @@ -255,6 +261,8 @@ public void alterDatabase(String name, List<PropertyChange> changes, boolean ign
if (!ignoreIfNotExists) {
throw new DatabaseNotExistException(name);
}
} catch (ForbiddenException e) {
throw NoPermissionException.createDatabaseNoPermissionException(name, e);
}
}

Expand Down Expand Up @@ -304,7 +312,7 @@ public void renameTable(Identifier fromTable, Identifier toTable, boolean ignore
throw new TableNotExistException(fromTable);
}
} catch (ForbiddenException e) {
throw new TableNoPermissionException(fromTable);
throw NoPermissionException.createTableNoPermissionException(fromTable, e);
} catch (AlreadyExistsException e) {
throw new TableAlreadyExistException(toTable);
}
Expand All @@ -321,7 +329,7 @@ public void alterTable(
throw new TableNotExistException(identifier);
}
} catch (ForbiddenException e) {
throw new TableNoPermissionException(identifier);
throw NoPermissionException.createTableNoPermissionException(identifier, e);
}
}

Expand All @@ -337,7 +345,7 @@ public void dropTable(Identifier identifier, boolean ignoreIfNotExists)
throw new TableNotExistException(identifier);
}
} catch (ForbiddenException e) {
throw new TableNoPermissionException(identifier);
throw NoPermissionException.createTableNoPermissionException(identifier, e);
}
}

Expand Down Expand Up @@ -400,7 +408,7 @@ protected TableSchema getDataTableSchema(Identifier identifier) throws TableNotE
} catch (NoSuchResourceException e) {
throw new TableNotExistException(identifier);
} catch (ForbiddenException e) {
throw new TableNoPermissionException(identifier, e);
throw NoPermissionException.createTableNoPermissionException(identifier, e);
}
}

Expand Down

0 comments on commit 303fd1e

Please sign in to comment.