Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): Improve CLI commands with better error handling and output formatting #6573

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,15 @@ public void handle() {
result = client.loadCatalog(this.catalog);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
return; // Stop execution after handling error
} catch (NoSuchCatalogException err) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
return; // Stop execution after handling error
} catch (Exception exp) {
exitWithError(exp.getMessage());
return; // Stop execution after handling error
}

// **Null check before accessing auditInfo()**
if (result != null && result.auditInfo() != null) {
// Only check if result is null
if (result != null) {
displayAuditInfo(result.auditInfo());
} else {
exitWithError("Audit information is not available for this catalog.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class FilesetDetails extends Command {
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schenma.
* @param schema The name of the schema.
* @param fileset The name of the fileset.
*/
public FilesetDetails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.gravitino.cli.commands;

import java.util.Map;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
Expand Down Expand Up @@ -49,31 +48,19 @@ public ListCatalogProperties(CommandContext context, String metalake, String cat
/** List the properties of a catalog. */
@Override
public void handle() {
Catalog gCatalog = null;
try {
GravitinoClient client = buildClient(metalake);
Catalog gCatalog = client.loadCatalog(catalog);

try (GravitinoClient client = buildClient(metalake)) { // Ensures client is closed
gCatalog = client.loadCatalog(catalog);
if (gCatalog != null) {
printProperties(gCatalog.properties());
}
} catch (NoSuchMetalakeException err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh No. This code won't work.
Checking gCatalog on line 54 before it is declared on line 59?
Throw NoSuchMetalakeException while metalake is used on line 52?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will loadCatalog throw NoSuchMetalakeException?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On checking loadCatalog it will generally not throw NoSuchMetalakeException, and hence removing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code buildClient(metalake); may throw this exception, right?
You moved that line out of the try block for unknown reason.
Now you know why I'm concerned?

exitWithError(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (Exception exp) {
exitWithError(exp.getMessage());
return;
}

if (gCatalog == null) { // Null check before accessing properties
exitWithError("Failed to load catalog.");
return;
}

Map<String, String> properties = gCatalog.properties();
if (properties == null || properties.isEmpty()) {
exitWithError("No properties found for the catalog.");
return;
}
printProperties(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.cli.commands;

import com.google.common.base.Joiner;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.exceptions.NoSuchTableException;
import org.apache.gravitino.rel.Column;
import com.google.common.base.Joiner;

/** Displays the details of a table's columns. */
public class ListColumns extends TableCommand {
Expand All @@ -48,53 +47,57 @@ public ListColumns(
this.table = table;
}

/** Displays the details of a table's columns. */
@Override
public void handle() {
Column[] columns = null;
/** Displays the details of a table's columns. */
@Override
public void handle() {
try {
NameIdentifier name = NameIdentifier.of(schema, table);
Column[] columns = tableCatalog().loadTable(name).columns();

try {
NameIdentifier name = NameIdentifier.of(schema, table);
columns = tableCatalog().loadTable(name).columns();
} catch (NoSuchTableException noSuchTableException) {
exitWithError(
ErrorMessages.UNKNOWN_TABLE + Joiner.on(".").join(metalake, catalog, schema, table));
return;
} catch (Exception exp) {
exitWithError(exp.getMessage());
return;
}
if (columns != null && columns.length > 0) {
StringBuilder all = new StringBuilder();
boolean hasAutoIncrement = false;

// Null / empty check before processing columns
if (columns == null || columns.length == 0) {
exitWithError("No columns found for table: " + table);
return;
}
// Check if any column supports auto-increment
for (Column column : columns) {
if (column != null && column.autoIncrement()) {
hasAutoIncrement = true;
break;
}
}

StringBuilder all = new StringBuilder();
all.append("name,datatype,comment,nullable,auto_increment").append(System.lineSeparator());
all.append("name,datatype,comment,nullable");
if (hasAutoIncrement) {
all.append(",auto_increment");
}
all.append(System.lineSeparator());

for (Column column : columns) {
if (column == null) continue; // Skip any unexpected null columns
for (Column column : columns) {
if (column == null) {
continue;
}

String name = column.name();
String dataType = column.dataType() != null ? column.dataType().simpleString() : "UNKNOWN";
String comment = column.comment() != null ? column.comment() : "N/A";
String nullable = column.nullable() ? "true" : "false";
String autoIncrement = column.autoIncrement() ? "true" : "false";
StringBuilder columnDetails = new StringBuilder();
columnDetails.append(column.name()).append(",");
columnDetails.append(column.dataType() != null ? column.dataType().simpleString() : "UNKNOWN").append(",");
columnDetails.append(column.comment() != null ? column.comment() : "N/A").append(",");
columnDetails.append(column.nullable() ? "true" : "false");

all.append(name)
.append(",")
.append(dataType)
.append(",")
.append(comment)
.append(",")
.append(nullable)
.append(",")
.append(autoIncrement)
.append(System.lineSeparator());
}
if (hasAutoIncrement) {
columnDetails.append(",").append(column.autoIncrement() ? "true" : "");
}

printResults(all.toString());
}
all.append(columnDetails).append(System.lineSeparator());
}

printResults(all.toString());
} else {
exitWithError("No columns found for the specified table.");
}
} catch (NoSuchTableException noSuchTableException) {
exitWithError(ErrorMessages.UNKNOWN_TABLE + " " + Joiner.on(".").join(metalake, catalog, schema, table));
} catch (Exception exp) {
exitWithError("An error occurred while retrieving column details: " + exp.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.apache.gravitino.cli.commands;

import java.util.Collections;
import java.util.Map;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
Expand Down Expand Up @@ -60,27 +60,28 @@ public ListFilesetProperties(
@Override
public void handle() {
Fileset gFileset = null;
NameIdentifier name = null;

try {
name = NameIdentifier.of(schema, fileset);
} catch (Exception exp) {
exitWithError("Invalid schema or fileset name: " + exp.getMessage());
return;
}
try (GravitinoClient client = buildClient(metalake)) {
gFileset = client.loadCatalog(catalog).asFilesetCatalog().loadFileset(name);
NameIdentifier name = NameIdentifier.of(schema, fileset);

try (GravitinoClient client = buildClient(metalake)) {
gFileset = client.loadCatalog(catalog).asFilesetCatalog().loadFileset(name);
}
} catch (IllegalArgumentException exp) {
exitWithError("Invalid schema or fileset name: " + exp.getMessage());
return;
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
return;
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
return;
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (NoSuchSchemaException err) {
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
return;
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
return;
} catch (Exception exp) {
exitWithError(exp.getMessage());
return;
exitWithError(exp.getMessage());
return;
}

// Null check for gFileset before accessing its properties
Expand All @@ -89,11 +90,6 @@ public void handle() {
return;
}

Map<String, String> properties = gFileset.properties();
// Use an empty map if properties is null to avoid NPE later on
if (properties == null) {
properties = Collections.emptyMap();
}
printProperties(properties);
printProperties(gFileset.properties() != null ? gFileset.properties() : Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import java.util.Map;

import org.apache.gravitino.Metalake;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
Expand All @@ -45,20 +46,23 @@ public ListMetalakeProperties(CommandContext context, String metalake) {
/** List the properties of a metalake. */
@Override
public void handle() {
Metalake gMetalake = null;

try (GravitinoAdminClient client = buildAdminClient()) { // Ensure resource cleanup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely to close the client too early.

Metalake gMetalake = client.loadMetalake(metalake);
gMetalake = client.loadMetalake(metalake);

if (gMetalake == null) {
exitWithError("Metalake not found: " + metalake);
return;
}

Map<String, String> properties = gMetalake.properties();
printProperties(properties);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

Map<String, String> properties = gMetalake.properties();
printProperties(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import java.util.Map;

import org.apache.gravitino.Schema;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
Expand Down Expand Up @@ -54,16 +55,10 @@ public ListSchemaProperties(
/** List the properties of a schema. */
@Override
public void handle() {
try (GravitinoClient client = buildClient(metalake)) { // Ensure resource cleanup
Schema gSchema = client.loadCatalog(catalog).asSchemas().loadSchema(schema);

if (gSchema == null) {
exitWithError("Schema not found: " + schema);
return;
}
Schema gSchema = null;

Map<String, String> properties = gSchema.properties();
printProperties(properties);
try (GravitinoClient client = buildClient(metalake)) { // Ensure resource cleanup
gSchema = client.loadCatalog(catalog).asSchemas().loadSchema(schema);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException err) {
Expand All @@ -73,5 +68,13 @@ public void handle() {
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

if (gSchema == null) {
exitWithError("Schema not found: " + schema);
return;
}

Map<String, String> properties = gSchema.properties();
printProperties(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ListTables extends TableCommand {
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schenma.
* @param schema The name of the schema.
*/
public ListTables(CommandContext context, String metalake, String catalog, String schema) {
super(context, metalake, catalog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,14 @@ public void handle() {

} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (NoSuchSchemaException err) {
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
return;
} catch (NoSuchTopicException err) {
exitWithError(ErrorMessages.UNKNOWN_TOPIC);
return;
} catch (Exception exp) {
exitWithError("An unexpected error occurred: " + exp.getMessage());
return;
exitWithError(exp.getMessage());
}

Map<String, String> properties = gTopic.properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SchemaAudit extends AuditCommand {
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schenma.
* @param schema The name of the schema.
*/
public SchemaAudit(CommandContext context, String metalake, String catalog, String schema) {
super(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SchemaDetails extends Command {
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schenma.
* @param schema The name of the schema.
*/
public SchemaDetails(CommandContext context, String metalake, String catalog, String schema) {
super(context);
Expand Down
Loading
Loading