Skip to content

Commit

Permalink
using consistently 'abort' in case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Weismueller committed Aug 6, 2018
1 parent 569dc71 commit 44abcec
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

import psycopg2
from flask import current_app
from flask import current_app, jsonify, make_response
from flask_restful import fields, Resource, marshal_with, reqparse, abort
from flask_sqlalchemy import SQLAlchemy

Expand Down Expand Up @@ -76,7 +76,9 @@ def delete(self, id):

entity = DBInstance.query.get(id)
if not entity:
return {'status': 'failed', 'msg': 'Postgraas instance {} does not exist'.format(id)}, 404
abort(404, status='failed',
msg='Postgraas instance {} does not exist'.format(id)
)

try:
with psycopg2.connect(
Expand All @@ -89,10 +91,9 @@ def delete(self, id):
pass
except Exception as ex:
return_code = 401 if 'authentication failed' in str(ex) else 500
return {
'status': 'failed',
'msg': 'Could not connect to postgres instance: {}'.format(str(ex))
}, return_code
abort(return_code, status='failed',
msg='Could not connect to postgres instance: {}'.format(str(ex))
)

if not current_app.postgraas_backend.exists(entity):
logger.warning(
Expand All @@ -109,7 +110,7 @@ def delete(self, id):
current_app.postgraas_backend.delete(entity)
except PostgraasApiException as e:
logger.warning("error deleting container {}: {}".format(entity.container_id, str(e)))
return {'status': 'failed', 'msg': str(e)}, 500
abort(500, status='failed', msg=str(e))
db.session.delete(entity)
db.session.commit()
return {'status': 'success', 'msg': 'deleted postgraas instance'}
Expand Down Expand Up @@ -173,7 +174,7 @@ def post(self):
try:
db_entry.container_id = current_app.postgraas_backend.create(db_entry, db_credentials)
except PostgraasApiException as e:
return {'msg': str(e)}, 500
abort(500, msg=str(e))

if '@' not in args['db_username']:
try:
Expand Down

0 comments on commit 44abcec

Please sign in to comment.