Minor: Improve exception handling in Superset (#14371)

This commit is contained in:
Mayur Singal 2023-12-13 18:33:59 +05:30 committed by GitHub
parent 9d5c3a490f
commit 35d4c64e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 120 additions and 67 deletions

View File

@ -123,9 +123,12 @@ class SupersetAPISource(SupersetSourceMixin):
) -> Iterable[Either[CreateChartRequest]]: ) -> Iterable[Either[CreateChartRequest]]:
"""Method to fetch charts linked to dashboard""" """Method to fetch charts linked to dashboard"""
for chart_id in self._get_charts_of_dashboard(dashboard_details): for chart_id in self._get_charts_of_dashboard(dashboard_details):
try:
chart_json = self.all_charts.get(chart_id) chart_json = self.all_charts.get(chart_id)
if not chart_json: if not chart_json:
logger.warning(f"chart details for id: {chart_id} not found, skipped") logger.warning(
f"chart details for id: {chart_id} not found, skipped"
)
continue continue
chart = CreateChartRequest( chart = CreateChartRequest(
name=chart_json.id, name=chart_json.id,
@ -136,6 +139,14 @@ class SupersetAPISource(SupersetSourceMixin):
service=self.context.dashboard_service, service=self.context.dashboard_service,
) )
yield Either(right=chart) yield Either(right=chart)
except Exception as exc: # pylint: disable=broad-except
yield Either(
left=StackTraceError(
name=chart_json.id,
error=f"Error creating chart [{chart_json.id} - {chart_json.slice_name}]: {exc}",
stack_trace=traceback.format_exc(),
)
)
def _get_datasource_fqn( def _get_datasource_fqn(
self, datasource_id: str, db_service_entity: DatabaseService self, datasource_id: str, db_service_entity: DatabaseService
@ -166,7 +177,7 @@ class SupersetAPISource(SupersetSourceMixin):
service_name=db_service_entity.name.__root__, service_name=db_service_entity.name.__root__,
) )
return dataset_fqn return dataset_fqn
except KeyError as err: except Exception as err:
logger.debug(traceback.format_exc()) logger.debug(traceback.format_exc())
logger.warning( logger.warning(
f"Failed to fetch Datasource with id [{datasource_id}]: {err}" f"Failed to fetch Datasource with id [{datasource_id}]: {err}"

View File

@ -71,15 +71,27 @@ class SupersetDBSource(SupersetSourceMixin):
this step is done because fetch_total_charts api fetches all this step is done because fetch_total_charts api fetches all
the required information which is not available in fetch_charts_with_id api the required information which is not available in fetch_charts_with_id api
""" """
try:
charts = self.engine.execute(FETCH_ALL_CHARTS) charts = self.engine.execute(FETCH_ALL_CHARTS)
for chart in charts: for chart in charts:
chart_detail = FetchChart(**chart) chart_detail = FetchChart(**chart)
self.all_charts[chart_detail.id] = chart_detail self.all_charts[chart_detail.id] = chart_detail
except Exception as err:
logger.debug(traceback.format_exc())
logger.warning(f"Failed to fetch chart list due to - {err}]")
def get_column_list(self, table_name: str) -> Iterable[FetchChart]: def get_column_list(self, table_name: str) -> Iterable[FetchChart]:
try:
if table_name:
sql_query = sql.text(FETCH_COLUMN.format(table_name=table_name.lower())) sql_query = sql.text(FETCH_COLUMN.format(table_name=table_name.lower()))
col_list = self.engine.execute(sql_query) col_list = self.engine.execute(sql_query)
return [FetchColumn(**col) for col in col_list] return [FetchColumn(**col) for col in col_list]
except Exception as err:
logger.debug(traceback.format_exc())
logger.warning(
f"Failed to fetch column name list for table: [{table_name} due to - {err}]"
)
return []
def get_dashboards_list(self) -> Iterable[FetchDashboard]: def get_dashboards_list(self) -> Iterable[FetchDashboard]:
""" """
@ -93,6 +105,7 @@ class SupersetDBSource(SupersetSourceMixin):
self, dashboard_details: FetchDashboard self, dashboard_details: FetchDashboard
) -> Iterable[Either[CreateDashboardRequest]]: ) -> Iterable[Either[CreateDashboardRequest]]:
"""Method to Get Dashboard Entity""" """Method to Get Dashboard Entity"""
try:
dashboard_request = CreateDashboardRequest( dashboard_request = CreateDashboardRequest(
name=dashboard_details.id, name=dashboard_details.id,
displayName=dashboard_details.dashboard_title, displayName=dashboard_details.dashboard_title,
@ -110,6 +123,17 @@ class SupersetDBSource(SupersetSourceMixin):
) )
yield Either(right=dashboard_request) yield Either(right=dashboard_request)
self.register_record(dashboard_request=dashboard_request) self.register_record(dashboard_request=dashboard_request)
except Exception as exc:
yield Either(
left=StackTraceError(
name=dashboard_details.id,
error=(
f"Error yielding Dashboard [{dashboard_details.id} "
f"- {dashboard_details.dashboard_title}]: {exc}"
),
stack_trace=traceback.format_exc(),
)
)
def _get_datasource_fqn_for_lineage( def _get_datasource_fqn_for_lineage(
self, chart_json: FetchChart, db_service_entity: DatabaseService self, chart_json: FetchChart, db_service_entity: DatabaseService
@ -127,9 +151,13 @@ class SupersetDBSource(SupersetSourceMixin):
Metod to fetch charts linked to dashboard Metod to fetch charts linked to dashboard
""" """
for chart_id in self._get_charts_of_dashboard(dashboard_details): for chart_id in self._get_charts_of_dashboard(dashboard_details):
try:
chart_json = self.all_charts.get(chart_id) chart_json = self.all_charts.get(chart_id)
if not chart_json: if not chart_json:
logger.warning(f"chart details for id: {chart_id} not found, skipped") logger.warning(
f"chart details for id: {chart_id} not found, skipped"
)
continue continue
chart = CreateChartRequest( chart = CreateChartRequest(
name=chart_json.id, name=chart_json.id,
@ -140,6 +168,14 @@ class SupersetDBSource(SupersetSourceMixin):
service=self.context.dashboard_service, service=self.context.dashboard_service,
) )
yield Either(right=chart) yield Either(right=chart)
except Exception as exc:
yield Either(
left=StackTraceError(
name=chart_json.id,
error=f"Error yielding Chart [{chart_json.id} - {chart_json.slice_name}]: {exc}",
stack_trace=traceback.format_exc(),
)
)
def _get_database_name( def _get_database_name(
self, sqa_str: str, db_service_entity: DatabaseService self, sqa_str: str, db_service_entity: DatabaseService

View File

@ -121,6 +121,7 @@ class SupersetSourceMixin(DashboardServiceSource):
""" """
Method to fetch chart ids linked to dashboard Method to fetch chart ids linked to dashboard
""" """
try:
raw_position_data = dashboard_details.position_json raw_position_data = dashboard_details.position_json
if raw_position_data: if raw_position_data:
position_data = json.loads(raw_position_data) position_data = json.loads(raw_position_data)
@ -129,6 +130,11 @@ class SupersetSourceMixin(DashboardServiceSource):
for key, value in position_data.items() for key, value in position_data.items()
if key.startswith("CHART-") and value.get("meta", {}).get("chartId") if key.startswith("CHART-") and value.get("meta", {}).get("chartId")
] ]
except Exception as err:
logger.debug(traceback.format_exc())
logger.warning(
f"Failed to charts of dashboard {dashboard_details.id} due to {err}"
)
return [] return []
def yield_dashboard_lineage_details( def yield_dashboard_lineage_details(
@ -146,6 +152,7 @@ class SupersetSourceMixin(DashboardServiceSource):
for chart_id in self._get_charts_of_dashboard(dashboard_details): for chart_id in self._get_charts_of_dashboard(dashboard_details):
chart_json = self.all_charts.get(chart_id) chart_json = self.all_charts.get(chart_id)
if chart_json: if chart_json:
try:
datasource_fqn = self._get_datasource_fqn_for_lineage( datasource_fqn = self._get_datasource_fqn_for_lineage(
chart_json, db_service_entity chart_json, db_service_entity
) )
@ -155,7 +162,6 @@ class SupersetSourceMixin(DashboardServiceSource):
entity=Table, entity=Table,
fqn=datasource_fqn, fqn=datasource_fqn,
) )
try:
datamodel_fqn = fqn.build( datamodel_fqn = fqn.build(
self.metadata, self.metadata,
entity_type=DashboardDataModel, entity_type=DashboardDataModel,