From e349d641ec13801a8a3c205133da18fdb94b02cf Mon Sep 17 00:00:00 2001 From: PengfeiLi0218 Date: Tue, 29 Jan 2019 19:52:21 +0800 Subject: [PATCH] Error handling for KPI Dashboard of API Manager if CanGetConsumers, CanGetAnyUser is not present #65 --- apimanager/config/views.py | 3 +++ apimanager/consumers/views.py | 9 ++++++++ apimanager/customers/views.py | 5 +++++ apimanager/entitlementrequests/views.py | 8 +++++++ apimanager/metrics/views.py | 30 +++++++++++++++++++++++++ apimanager/obp/views.py | 7 ++++++ 6 files changed, 62 insertions(+) diff --git a/apimanager/config/views.py b/apimanager/config/views.py index 7365e0eb..cd4af7e9 100644 --- a/apimanager/config/views.py +++ b/apimanager/config/views.py @@ -25,6 +25,9 @@ def get_context_data(self, **kwargs): except APIError as err: messages.error(self.request, err) config = {} + except: + messages.error(self.request, "Unknown Error") + config = {} context.update({ 'config_json': json.dumps(config, indent=4), diff --git a/apimanager/consumers/views.py b/apimanager/consumers/views.py index ea8e607b..fe7cefb0 100644 --- a/apimanager/consumers/views.py +++ b/apimanager/consumers/views.py @@ -82,6 +82,8 @@ def get_context_data(self, **kwargs): consumers = self.scrub(consumers) except APIError as err: messages.error(self.request, err) + except: + messages.error(self.request, "Unknown") sorted_consumers = sorted( consumers, key=lambda consumer: consumer['created'], reverse=True) @@ -128,6 +130,9 @@ def form_valid(self, form): except APIError as err: messages.error(self.request, err) return super(DetailView, self).form_invalid(form) + except: + messages.error(self.request, "Unknown") + return super(DetailView, self).form_invalid(form) msg = 'calls limit of consumer {} has been updated successfully.'.format( data['consumer_id']) @@ -154,6 +159,8 @@ def get_context_data(self, **kwargs): except APIError as err: messages.error(self.request, err) + except: + messages.error(self.request, "Unknown") context.update({ 'consumer': consumer @@ -175,6 +182,8 @@ def get_redirect_url(self, *args, **kwargs): messages.success(self.request, self.success) except APIError as err: messages.error(self.request, err) + except: + messages.error(self.request, "Unknown") urlpath = self.request.POST.get('next', reverse('consumers-index')) query = self.request.GET.urlencode() diff --git a/apimanager/customers/views.py b/apimanager/customers/views.py index a6905085..e942d458 100644 --- a/apimanager/customers/views.py +++ b/apimanager/customers/views.py @@ -35,6 +35,8 @@ def get_form(self, *args, **kwargs): fields['bank_id'].choices = self.api.get_bank_id_choices() except APIError as err: messages.error(self.request, err) + except: + messages.error(self.request, "Unknown Error") fields['last_ok_date'].initial =\ datetime.datetime.now().strftime(settings.API_DATETIMEFORMAT) return form @@ -76,6 +78,9 @@ def form_valid(self, form): except APIError as err: messages.error(self.request, err) return super(CreateView, self).form_invalid(form) + except: + messages.error(self.request, "Unknown Error") + return super(CreateView, self).form_invalid(form) msg = 'Customer number {} for user {} has been created successfully!'.format( # noqa result['customer_number'], data['username']) messages.success(self.request, msg) diff --git a/apimanager/entitlementrequests/views.py b/apimanager/entitlementrequests/views.py index 63091f62..304c7866 100644 --- a/apimanager/entitlementrequests/views.py +++ b/apimanager/entitlementrequests/views.py @@ -39,6 +39,8 @@ def get_context_data(self, **kwargs): entitlement_requests = self.scrub(entitlement_requests) except APIError as err: messages.error(self.request, err) + except: + messages.error(self.request, "Unknown Error") context.update({ 'entitlementrequests': entitlement_requests, @@ -60,6 +62,8 @@ def post(self, request, *args, **kwargs): messages.success(request, msg) except APIError as err: messages.error(request, err) + except: + messages.error(self.request, "Unknown Error") redirect_url = request.POST.get('next', reverse('entitlementrequests-index')) return HttpResponseRedirect(redirect_url) @@ -83,6 +87,8 @@ def post(self, request, *args, **kwargs): messages.success(request, msg) except APIError as err: messages.error(request, err) + except: + messages.error(self.request, "Unknown Error") try: urlpath = '/entitlement-requests/{}'.format(request.POST.get('entitlement_request_id', '')) @@ -92,6 +98,8 @@ def post(self, request, *args, **kwargs): messages.success(request, msg) except APIError as err: messages.error(request, err) + except: + messages.error(self.request, "Unknown Error") redirect_url = request.POST.get('next', reverse('entitlementrequests-index')) return HttpResponseRedirect(redirect_url) \ No newline at end of file diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index 5b07a3aa..f942edea 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -264,6 +264,8 @@ def get_aggregate_metrics(self, cleaned_data, from_date, to_date): average_response_time = metrics[0]["average_response_time"] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') else: urlpath = '/management/aggregate-metrics?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( @@ -277,6 +279,8 @@ def get_aggregate_metrics(self, cleaned_data, from_date, to_date): average_response_time = metrics[0]["average_response_time"] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') to_date = datetime.datetime.strptime(to_date, API_DATEFORMAT) @@ -324,6 +328,8 @@ def get_active_apps(self, cleaned_data, from_date, to_date): active_apps_list = list(apps) except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') else: urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( from_date, to_date, ",".join(EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) @@ -333,6 +339,8 @@ def get_active_apps(self, cleaned_data, from_date, to_date): active_apps_list = list(apps) except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') return active_apps_list @@ -349,6 +357,8 @@ def get_total_number_of_apps(self, cleaned_data, from_date, to_date): apps_list = apps["list"] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') for app in apps_list: app_created_date = datetime.datetime.strptime(app["created"], '%Y-%m-%dT%H:%M:%SZ') @@ -418,6 +428,8 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ): sum += result except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') date_from = date_to date_list.append(date_from) @@ -435,6 +447,8 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ): sum += result except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') date_from = date_to date_list.append(date_from) @@ -575,6 +589,8 @@ def get_users_cansearchwarehouse(self): users = api.get(urlpath) except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') else: try: @@ -586,6 +602,8 @@ def get_users_cansearchwarehouse(self): # fail gracefully in case API provides new structure except KeyError as err: messages.error(self.request, 'KeyError: {}'.format(err)) + except: + error_once_only(self.request, 'Unknown Error') user_email_cansearchwarehouse = dict(zip(users_with_cansearchwarehouse, email_with_cansearchwarehouse)) number_of_users_with_cansearchwarehouse = len(user_email_cansearchwarehouse) @@ -601,6 +619,8 @@ def get_top_apis(self, cleaned_data, from_date, to_date): top_apis = api.get(urlpath)['top_apis'] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') else: urlpath = '/management/metrics/top-apis?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( from_date, to_date, ",".join(EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) @@ -609,6 +629,8 @@ def get_top_apis(self, cleaned_data, from_date, to_date): top_apis = api.get(urlpath)['top_apis'] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') for api in top_apis: if api['Implemented_by_partial_function'] == "": @@ -630,6 +652,8 @@ def get_top_warehouse_calls(self, cleaned_data, from_date, to_date): top_warehouse_calls.append(api) except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') return top_warehouse_calls def get_top_apps_using_warehouse(self, from_date, to_date): @@ -644,6 +668,8 @@ def get_top_apps_using_warehouse(self, from_date, to_date): top_apps_using_warehouse = top_apps_using_warehouse["top_consumers"][:2] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') return top_apps_using_warehouse @@ -660,6 +686,8 @@ def median_time_to_first_api_call(self, from_date, to_date): apps_list = apps["list"] except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') for app in apps_list: created_date = datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ') @@ -684,6 +712,8 @@ def median_time_to_first_api_call(self, from_date, to_date): except APIError as err: error_once_only(self.request, err) + except: + error_once_only(self.request, 'Unknown Error') if times_to_first_call: median = statistics.median(times_to_first_call) diff --git a/apimanager/obp/views.py b/apimanager/obp/views.py index 5a715102..0cea5ad5 100644 --- a/apimanager/obp/views.py +++ b/apimanager/obp/views.py @@ -30,6 +30,8 @@ def login_to_django(self): data = api.get('/users/current') except APIError as err: messages.error(self.request, err) + except: + messages.error(self.request, 'Unknown Error') return False else: userid = data['user_id'] or data['email'] @@ -66,6 +68,9 @@ def get_redirect_url(self, *args, **kwargs): except AuthenticatorError as err: messages.error(self.request, err) return reverse('home') + except: + messages.error(self.request, 'Unknown Error') + return reverse('home') else: self.request.session['obp'] = { 'authenticator': 'obp.oauth.OAuthAuthenticator', @@ -89,6 +94,8 @@ def get_redirect_url(self, *args, **kwargs): authenticator.set_access_token(authorization_url) except AuthenticatorError as err: messages.error(self.request, err) + except: + messages.error(self.request, 'Unknown Error') else: session_data['authenticator_kwargs'] = { 'token': authenticator.token,