Skip to content

Commit

Permalink
Error handling in DataSetChangeAPI view
Browse files Browse the repository at this point in the history
  • Loading branch information
kaapstorm committed Dec 6, 2023
1 parent e9e2207 commit 591857f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 10 additions & 2 deletions hq_superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ def get_hq_database():
from superset import db
from superset.models.core import Database

# Todo; get actual DB once that's implemented
return db.session.query(Database).filter_by(database_name=HQ_DB_CONNECTION_NAME).one()
try:
hq_db = (
db.session
.query(Database)
.filter_by(database_name=HQ_DB_CONNECTION_NAME)
.one()
)
except sqlalchemy.orm.exc.NoResultFound as err:
raise CCHQApiException('CommCare HQ database missing') from err
return hq_db


def get_schema_name_for_domain(domain):
Expand Down
21 changes: 16 additions & 5 deletions hq_superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import superset
from flask import Response, abort, flash, g, redirect, request, url_for
from flask_appbuilder import expose
from flask_appbuilder.baseviews import expose_api
from flask_appbuilder.security.decorators import has_access, permission_name
from superset import db
from superset.connectors.sqla.models import SqlaTable
Expand All @@ -15,8 +16,13 @@
DatasetForbiddenError,
DatasetNotFoundError,
)
from superset.extensions import csrf
from superset.superset_typing import FlaskResponse
from superset.views.base import BaseSupersetView, json_error_response
from superset.views.base import (
BaseSupersetView,
handle_api_exception,
json_error_response,
)

from .hq_domain import user_domains
from .models import DataSetChange
Expand Down Expand Up @@ -217,10 +223,11 @@ class DataSetChangeAPI(BaseSupersetView):

def __init__(self):
self.route_base = '/hq_webhook'
self.default_view = 'post'
self.default_view = 'post_dataset_change'
super().__init__()

@expose('/change/', methods=['POST'])
# http://localhost:8088/hq_webhook/change/
@expose_api(url='/change/', methods=('POST',))
# TODO: Authenticate
# e.g. superset.views.datasource.views.Datasource:
# @event_logger.log_this_with_context(
Expand All @@ -229,8 +236,9 @@ def __init__(self):
# )
# @has_access_api
# @api
# @handle_api_exception
def post(self) -> FlaskResponse:
@handle_api_exception
@csrf.exempt
def post_dataset_change(self) -> FlaskResponse:
if request.content_length > self.MAX_REQUEST_LENGTH:
return json_error_response(
HTTPStatus.REQUEST_ENTITY_TOO_LARGE.description,
Expand All @@ -255,3 +263,6 @@ def post(self) -> FlaskResponse:
str(err),
status=HTTPStatus.BAD_REQUEST.value,
)
# `@handle_api_exception` will return other exceptions as JSON
# with status code 500, e.g.
# {"error": "CommCare HQ database missing"}

0 comments on commit 591857f

Please sign in to comment.