Skip to content

Commit

Permalink
chore: refine error messageg
Browse files Browse the repository at this point in the history
  • Loading branch information
xianml committed Sep 2, 2024
1 parent 6191b4d commit b0fd1c9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/bentoml/_internal/cloud/bentocloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def pull_model(model_tag: Tag):
with response_ctx as response:
if response.status_code != 200:
raise BentoMLException(
f'Failed to download bento "{_tag}": {response.text}'
f'Failed to download bento "{_tag}": {response.status_code}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
Expand Down
2 changes: 1 addition & 1 deletion src/bentoml/_internal/cloud/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_context(self, context: t.Optional[str] = None) -> CloudClientContext:
if ctx.name == context:
return ctx
raise CloudRESTApiClientError(
f"BentoCloud API token for {context} cloud context is required.",
f"No cloud context {context} found",
error_code=HTTPStatus.UNAUTHORIZED,
)

Expand Down
2 changes: 1 addition & 1 deletion src/bentoml_cli/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def login(endpoint: str, api_token: str) -> None: # type: ignore (not accessed)
encodedCallback = urllib.parse.quote(callback_server.callback_url)
authURL = f"{baseURL}?callback={encodedCallback}"
if Confirm.ask(
f"Please Enter Y or N to open [blue]{authURL}[/] in your browser..."
f"Please Enter Y to open [blue]{authURL}[/] in your browser..."
):
if webbrowser.open_new_tab(authURL):
rich.print(f"✅ Opened [blue]{authURL}[/] in your web browser.")
Expand Down
110 changes: 57 additions & 53 deletions src/bentoml_cli/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
def raise_deployment_config_error(err: BentoMLException, action: str) -> t.NoReturn:
if err.error_code == HTTPStatus.UNAUTHORIZED:
raise BentoMLException(
f"{err}\n* BentoCloud sign up: https://cloud.bentoml.com/\n"
"* Login with your API token: "
"https://docs.bentoml.com/en/latest/bentocloud/how-tos/manage-access-token.html"
f"{err}\n* BentoCloud API token is required for authorization. Run `bentoml cloud login` command to sign up or create a token"
) from None
raise BentoMLException(
f"Failed to {action} deployment due to invalid configuration: {err}"
Expand Down Expand Up @@ -609,30 +607,33 @@ def list_command( # type: ignore
output: t.Literal["json", "yaml", "table"],
) -> None:
"""List existing deployments on BentoCloud."""
d_list = Deployment.list(cluster=cluster, search=search)
res: list[dict[str, t.Any]] = [d.to_dict() for d in d_list]
if output == "table":
table = Table(box=None, expand=True)
table.add_column("Deployment", overflow="fold")
table.add_column("created_at", overflow="fold")
table.add_column("Bento", overflow="fold")
table.add_column("Status", overflow="fold")
table.add_column("Region", overflow="fold")
for info in d_list:
table.add_row(
info.name,
info.created_at,
info.get_bento(refetch=False),
info.get_status(refetch=False).status,
info.cluster,
)
console.print(table)
elif output == "json":
info = json.dumps(res, indent=2, default=str)
console.print_json(info)
else:
info = yaml.dump(res, indent=2, sort_keys=False)
console.print(Syntax(info, "yaml", background_color="default"))
try:
d_list = Deployment.list(cluster=cluster, search=search)
res: list[dict[str, t.Any]] = [d.to_dict() for d in d_list]
if output == "table":
table = Table(box=None, expand=True)
table.add_column("Deployment", overflow="fold")
table.add_column("created_at", overflow="fold")
table.add_column("Bento", overflow="fold")
table.add_column("Status", overflow="fold")
table.add_column("Region", overflow="fold")
for info in d_list:
table.add_row(
info.name,
info.created_at,
info.get_bento(refetch=False),
info.get_status(refetch=False).status,
info.cluster,
)
console.print(table)
elif output == "json":
info = json.dumps(res, indent=2, default=str)
console.print_json(info)
else:
info = yaml.dump(res, indent=2, sort_keys=False)
console.print(Syntax(info, "yaml", background_color="default"))
except BentoMLException as e:
raise_deployment_config_error(e, "list")


@deployment_command.command()
Expand All @@ -649,32 +650,35 @@ def list_instance_types( # type: ignore
output: t.Literal["json", "yaml", "table"],
) -> None:
"""List existing instance types in cluster on BentoCloud."""
d_list = Deployment.list_instance_types(cluster=cluster)
res: list[dict[str, t.Any]] = [d.to_dict() for d in d_list]
if output == "table":
table = Table(box=None, expand=True)
table.add_column("Name", overflow="fold")
table.add_column("Price", overflow="fold")
table.add_column("CPU", overflow="fold")
table.add_column("Memory", overflow="fold")
table.add_column("GPU", overflow="fold")
table.add_column("GPU Type", overflow="fold")
for info in d_list:
table.add_row(
info.name,
info.price,
info.cpu,
info.memory,
info.gpu,
info.gpu_type,
)
console.print(table)
elif output == "json":
info = json.dumps(res, indent=2, default=str)
console.print_json(info)
else:
info = yaml.dump(res, indent=2, sort_keys=False)
console.print(Syntax(info, "yaml", background_color="default"))
try:
d_list = Deployment.list_instance_types(cluster=cluster)
res: list[dict[str, t.Any]] = [d.to_dict() for d in d_list]
if output == "table":
table = Table(box=None, expand=True)
table.add_column("Name", overflow="fold")
table.add_column("Price", overflow="fold")
table.add_column("CPU", overflow="fold")
table.add_column("Memory", overflow="fold")
table.add_column("GPU", overflow="fold")
table.add_column("GPU Type", overflow="fold")
for info in d_list:
table.add_row(
info.name,
info.price,
info.cpu,
info.memory,
info.gpu,
info.gpu_type,
)
console.print(table)
elif output == "json":
info = json.dumps(res, indent=2, default=str)
console.print_json(info)
else:
info = yaml.dump(res, indent=2, sort_keys=False)
console.print(Syntax(info, "yaml", background_color="default"))
except BentoMLException as e:
raise_deployment_config_error(e, "list_instance_types")


def create_deployment(
Expand Down
77 changes: 39 additions & 38 deletions src/bentoml_cli/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,41 @@ def list(
output: t.Literal["json", "yaml", "table"],
):
"""List all secrets on BentoCloud."""
secrets = Secret.list(search=search)
if output == "table":
table = Table(box=None, expand=True)
table.add_column("Secret", overflow="fold")
table.add_column("Created_At", overflow="fold")
table.add_column("Mount_As", overflow="fold")
table.add_column("Keys", overflow="fold")
table.add_column("Path", overflow="fold")
try:
secrets = Secret.list(search=search)
if output == "table":
table = Table(box=None, expand=True)
table.add_column("Secret", overflow="fold")
table.add_column("Created_At", overflow="fold")
table.add_column("Mount_As", overflow="fold")
table.add_column("Keys", overflow="fold")
table.add_column("Path", overflow="fold")

for secret in secrets:
keys = [item.key for item in secret.content.items]
mountAs = secret.content.type
if mountAs == "env":
mountAs = "Environment Variable"
elif mountAs == "mountfile":
mountAs = "File"
table.add_row(
secret.name,
secret.created_at.strftime("%Y-%m-%d %H:%M:%S"),
mountAs,
", ".join(keys),
secret.content.path if secret.content.path else "-",
)
console.print(table)
elif output == "json":
res: t.List[dict[str, t.Any]] = [s.to_dict() for s in secrets]
info = json.dumps(res, indent=2, default=str)
console.print(info)
elif output == "yaml":
res: t.List[dict[str, t.Any]] = [s.to_dict() for s in secrets]
info = yaml.dump(res, indent=2, sort_keys=False)
console.print(Syntax(info, "yaml", background_color="default"))
for secret in secrets:
keys = [item.key for item in secret.content.items]
mountAs = secret.content.type
if mountAs == "env":
mountAs = "Environment Variable"
elif mountAs == "mountfile":
mountAs = "File"
table.add_row(
secret.name,
secret.created_at.strftime("%Y-%m-%d %H:%M:%S"),
mountAs,
", ".join(keys),
secret.content.path if secret.content.path else "-",
)
console.print(table)
elif output == "json":
res: t.List[dict[str, t.Any]] = [s.to_dict() for s in secrets]
info = json.dumps(res, indent=2, default=str)
console.print(info)
elif output == "yaml":
res: t.List[dict[str, t.Any]] = [s.to_dict() for s in secrets]
info = yaml.dump(res, indent=2, sort_keys=False)
console.print(Syntax(info, "yaml", background_color="default"))
except BentoMLException as e:
raise_secret_error(e, "list")


def parse_kvs_argument_callback(
Expand Down Expand Up @@ -137,10 +140,8 @@ def parse_from_file_argument_callback(
def raise_secret_error(err: BentoMLException, action: str) -> t.NoReturn:
if err.error_code == HTTPStatus.UNAUTHORIZED:
raise BentoMLException(
f"{err}\n* BentoCloud sign up: https://cloud.bentoml.com/\n"
"* Login with your API token: "
"https://docs.bentoml.com/en/latest/bentocloud/how-tos/manage-access-token.html"
)
f"{err}\n* BentoCloud API token is required for authorization. Run `bentoml cloud login` command to sign up or create a token"
) from None
raise BentoMLException(f"Failed to {action} secret due to: {err}")


Expand Down Expand Up @@ -232,7 +233,7 @@ def create(
key_vals=key_vals,
)
rich.print(f"Secret [green]{secret.name}[/] created successfully")
except Exception as e:
except BentoMLException as e:
raise_secret_error(e, "create")


Expand All @@ -248,7 +249,7 @@ def delete(name: str):
try:
Secret.delete(name=name)
rich.print(f"Secret [green]{name}[/] deleted successfully")
except Exception as e:
except BentoMLException as e:
raise_secret_error(e, "delete")


Expand Down Expand Up @@ -335,5 +336,5 @@ def apply(
key_vals=key_vals,
)
rich.print(f"Secret [green]{secret.name}[/] applied successfully")
except Exception as e:
except BentoMLException as e:
raise_secret_error(e, "apply")

0 comments on commit b0fd1c9

Please sign in to comment.