Commit 6ba29490 authored by Lionel Walter's avatar Lionel Walter
Browse files

Use better exceptions

parent 1506efa0
......@@ -2,6 +2,8 @@ from flask import Flask, send_from_directory, redirect
from kubernetes import config
from flask_restful import Api
from flasgger import Swagger
from kubernetes.config import ConfigException
from import_api_app.resources.JobList import JobList
from import_api_app.resources.JobStart import JobStart
from import_api_app.resources.JobStop import JobStop
......@@ -84,12 +86,12 @@ def create_app(test_config=None):
try:
# to be used when inside a kubernetes cluster
config.load_incluster_config()
except BaseException:
except ConfigException:
try:
# use .kube directory
# for local development
config.load_kube_config()
except BaseException:
except ConfigException:
app.logger.error("No kubernetes cluster defined")
return app
......@@ -4,6 +4,7 @@ from flasgger import swag_from
from import_api_app.utility import generate_helm_name
import import_api_app.helm as helm
import os
from subprocess import CalledProcessError
class HelmStart(Resource):
......@@ -45,6 +46,6 @@ class HelmStart(Resource):
'message': message,
'job_id': helm_name
}, 500
except BaseException:
except CalledProcessError:
current_app.logger.error("It was not possible to run the helm install command")
return {'error': 'Unexpected Helm error'}, 500
......@@ -2,6 +2,7 @@ from flask_restful import Resource, current_app
import import_api_app.helm as helm
from flasgger import swag_from
from import_api_app.utility import generate_helm_name
from subprocess import CalledProcessError
class HelmStop(Resource):
......@@ -33,6 +34,6 @@ class HelmStop(Resource):
'message': message,
'job_id': helm_name
}, 500
except BaseException:
except CalledProcessError:
current_app.logger.error("It was not possible to run the helm install command")
return {'error': 'Unexpected Helm error'}, 500
......@@ -4,6 +4,7 @@ from flasgger import swag_from
from import_api_app.utility import generate_helm_name
import import_api_app.helm as helm
import os
from subprocess import CalledProcessError
class JobStart(Resource):
......@@ -45,6 +46,6 @@ class JobStart(Resource):
'message': message,
'job_id': helm_name
}, 500
except BaseException:
except CalledProcessError:
current_app.logger.error("It was not possible to run the helm install command")
return {'error': 'Unexpected Helm error'}, 500
......@@ -2,6 +2,7 @@ from flask_restful import Resource, current_app
import import_api_app.helm as helm
from flasgger import swag_from
from import_api_app.utility import generate_helm_name
from subprocess import CalledProcessError
class JobStop(Resource):
......@@ -33,6 +34,6 @@ class JobStop(Resource):
'message': message,
'job_id': helm_name
}, 500
except BaseException:
except CalledProcessError:
current_app.logger.error("It was not possible to run the helm install command")
return {'error': 'Unexpected Helm error'}, 500
......@@ -2,6 +2,7 @@ from flask_restful import Resource, current_app, reqparse
from kafka import KafkaConsumer, KafkaAdminClient
from kafka.admin import NewTopic
import time
from kafka.errors import KafkaError
class KafkaTopics(Resource):
......@@ -21,7 +22,7 @@ class KafkaTopics(Resource):
try:
topics = consumer.topics()
except BaseException:
except KafkaError:
message = "Problem connecting to Kafka cluster"
current_app.logger.error(message)
return {'error': message}, 500
......@@ -65,7 +66,7 @@ class KafkaTopics(Resource):
)
try:
current_topics = consumer.topics()
except BaseException:
except KafkaError:
message = "Problem connecting to Kafka cluster"
current_app.logger.error(message)
return {'error': message}, 500
......@@ -77,7 +78,7 @@ class KafkaTopics(Resource):
try:
admin_client.delete_topics(topic_to_delete)
except BaseException:
except KafkaError:
message = "Problem when removing already existing topics :"+", ".join(topic_to_delete)
current_app.logger.error(message)
return {'error': message}, 500
......@@ -95,7 +96,7 @@ class KafkaTopics(Resource):
try:
admin_client.create_topics(new_topic_list)
except BaseException:
except KafkaError:
message = "Problem when creating new topics :"+", ".join(topics)
current_app.logger.error(message)
return {'error': message}, 500
......@@ -134,7 +135,7 @@ class KafkaTopics(Resource):
try:
admin_client.delete_topics(topics)
except BaseException:
except KafkaError:
message = "Problem when deleting topics (probably they don't exist) :"+", ".join(topics)
current_app.logger.error(message)
return {'error': message}, 500
......
......@@ -26,6 +26,8 @@ class ReadJobOptionsFromDrupal(Resource):
description: The job parameters
"""
# todo use more standard requests library
# (as in the write endpoint) instead of jsonapi_requests
api = jsonapi_requests.Api.config({
'API_ROOT': current_app.config['DRUPAL_API_URL'],
# 'AUTH': ('basic_auth_login', 'basic_auth_password'),
......@@ -39,7 +41,15 @@ class ReadJobOptionsFromDrupal(Resource):
endpoint = api.endpoint(
'paragraph/job_'+job_name+'/'+job_drupal_uuid
)
response = endpoint.get()
try:
response = endpoint.get()
except Exception:
current_app.logger.error(
"It was not possible to read from Drupal API"
)
return {
'error': 'It was not possible to read from Drupal API'
}, 500
result = {}
......
......@@ -55,7 +55,7 @@ class WriteJobResultToDrupal(Resource):
headers=headers,
data=json.dumps(data)
)
except BaseException:
except requests.exceptions.RequestException:
current_app.logger.error(
"It was not possible to write to Drupal API via the following url " + url
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment