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

fix: handling json unmarshall errors #78

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions commands/db_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def do_databases(shell, arg):
else:
utils.write_output("No databases found.")
except Exception as e:
raise RuntimeError(f"Error listing databases: {e}") from e
raise RuntimeError(f"{e}") from e

#command .use
def do_use(shell, arg):
Expand All @@ -41,7 +41,7 @@ def do_use(shell, arg):
else:
utils.write_output(f"Failed to switch to database '{arg}'.")
except Exception as e:
raise RuntimeError(f"Error switching database: {e}") from e
raise RuntimeError(f"{e}") from e

#comand .dbinfo
def do_dbinfo(shell, arg):
Expand All @@ -59,7 +59,7 @@ def do_dbinfo(shell, arg):
else:
utils.write_output("Error: Unable to fetch database information.")
except Exception as e:
raise RuntimeError(f"Error fetching database info: {e}") from e
raise RuntimeError(f"{e}") from e

# Command .dbsize
def do_dbsize(shell, arg):
Expand All @@ -75,7 +75,7 @@ def do_dbsize(shell, arg):
return
shell.query_output(output['data']['rows'], output['data']['columns'])
except Exception as e:
raise RuntimeError(f"Error fetching database size: {e}") from e
raise RuntimeError(f"{e}") from e

#command .create
def do_create(shell, arg):
Expand Down Expand Up @@ -108,7 +108,7 @@ def do_create(shell, arg):
shell.edgeSql.create_database(database_name)
utils.write_output(f"Database '{database_name}' created successfully.")
except Exception as e:
raise RuntimeError(f"Error creating database: {e}") from e
raise RuntimeError(f"{e}") from e

#command .destroy
def do_destroy(shell, arg):
Expand Down Expand Up @@ -145,4 +145,4 @@ def do_destroy(shell, arg):
else:
utils.write_output(f"Failed to destroy database '{database_name}'.")
except Exception as e:
raise RuntimeError(f"Error destroying database: {e}") from e
raise RuntimeError(f"{e}") from e
33 changes: 25 additions & 8 deletions edgesql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import utils_sql as sql
import requests
from http import HTTPStatus
import json

BASE_URL = 'https://api.azion.com/v4/edge_sql/databases'

Expand Down Expand Up @@ -100,7 +101,10 @@ def execute(self, buffer):

try:
response = requests.post(url, json=data, headers=self.__headers(), timeout=self.timeout)
json_data = response.json()
try:
json_data = response.json()
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON response: {e}. statusCode={response.status_code}") from e

if response.status_code == HTTPStatus.OK:
result_data = json_data.get('data', [])
Expand Down Expand Up @@ -137,7 +141,10 @@ def list_databases(self):
"""
try:
response = requests.get(self._base_url, headers=self.__headers(), timeout=self.timeout)
json_data = response.json()
try:
json_data = response.json()
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON response: {e}. statusCode={response.status_code}") from e

if response.status_code == HTTPStatus.OK: # 200
databases = json_data.get('results')
Expand Down Expand Up @@ -170,7 +177,10 @@ def set_current_database(self, database_name):
"""
try:
response = requests.get(self._base_url, headers=self.__headers(), timeout=self.timeout)
json_data = response.json()
try:
json_data = response.json()
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON response: {e}. statusCode={response.status_code}") from e

if response.status_code == HTTPStatus.OK: # 200
databases = json_data.get('results')
Expand Down Expand Up @@ -201,8 +211,10 @@ def get_database_id(self, database_name):
"""
try:
response = requests.get(self._base_url, headers=self.__headers(), timeout=self.timeout)
response.raise_for_status()
json_data = response.json()
try:
json_data = response.json()
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON response: {e}. statusCode={response.status_code}") from e

if response.status_code == HTTPStatus.OK: # 200
databases = json_data.get('results',[])
Expand Down Expand Up @@ -233,7 +245,10 @@ def get_database_info(self):
url = f'{self._base_url}/{self._current_database_id}'
try:
response = requests.get(url, headers=self.__headers(), timeout=self.timeout)
json_data = response.json()
try:
json_data = response.json()
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON response: {e}. statusCode={response.status_code}") from e

if response.status_code == HTTPStatus.OK: # 200
data = json_data.get('data')
Expand Down Expand Up @@ -296,7 +311,10 @@ def create_database(self, database_name):

try:
response = requests.post(self._base_url, json=data, headers=self.__headers(), timeout=self.timeout)
json_data = response.json()
try:
json_data = response.json()
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON response: {e}. statusCode={response.status_code}") from e

if response.status_code in [HTTPStatus.ACCEPTED, HTTPStatus.CREATED]: # 201/202
data = json_data.get('data')
Expand Down Expand Up @@ -345,7 +363,6 @@ def destroy_database(self, database_name):

try:
response = requests.delete(url, headers=self.__headers(), timeout=self.timeout)
#response.raise_for_status()

if response.status_code == HTTPStatus.ACCEPTED: # 202
if self._current_database_name == database_name:
Expand Down
Loading