diff --git a/app/accounts/api.py b/app/accounts/api.py index 76cf236..2a7edfa 100644 --- a/app/accounts/api.py +++ b/app/accounts/api.py @@ -121,7 +121,6 @@ def create_token(username, password): raise ValueError("Invalid credentials") if not account.confirmed: - print('ACCOUNT NOT CONFIRMED?') raise ValueError("Email not confirmed") return account.create_auth_token() diff --git a/app/api/blueprint.py b/app/api/blueprint.py index 488e8ec..0b005ca 100644 --- a/app/api/blueprint.py +++ b/app/api/blueprint.py @@ -1,5 +1,6 @@ from flask_restful import Api from marshmallow import ValidationError +from app.errors import NotPresentError from flask import Blueprint, jsonify @@ -89,6 +90,12 @@ def handle_value_error(e): return jsonify({'status': 'error', 'message': str(e)}), 422 +@api_bp.errorhandler(NotPresentError) +@api_bp.errorhandler(404) +def handle_not_present_error(e): + return jsonify({'status': 'error', 'message': str(e)}), 404 + + @api_bp.errorhandler(Exception) @api_bp.errorhandler(500) def handle_unknown_errors(e): diff --git a/app/api/resources/account.py b/app/api/resources/account.py index 5b4e66e..441dff2 100644 --- a/app/api/resources/account.py +++ b/app/api/resources/account.py @@ -86,24 +86,21 @@ class AccountListResource(Resource): @use_args(UserSchema(), locations=('json',)) @swag_from('swagger/create_account_spec.yaml') def post(self, args): - try: - created_account, emailtoken = accounts.create_account( - args['username'], - args['email'], - args['password']) - confirm_url = api.url_for( - AccountEmailTokenResource, - token=emailtoken, _external=True) - html = render_template( - 'activate_mail.html', - confirm_url=confirm_url) - send_email_task.delay( - args['email'], - 'ETF IoT Email confirmation', - html) - return UserSchema().dump(created_account), 201 - except ValueError: - abort(422, message='Account already exists', status='error') + created_account, emailtoken = accounts.create_account( + args['username'], + args['email'], + args['password']) + confirm_url = api.url_for( + AccountEmailTokenResource, + token=emailtoken, _external=True) + html = render_template( + 'activate_mail.html', + confirm_url=confirm_url) + send_email_task.delay( + args['email'], + 'ETF IoT Email confirmation', + html) + return UserSchema().dump(created_account), 201 class AccountEmailTokenResource(Resource): diff --git a/app/devices/api.py b/app/devices/api.py index 8b5fb7f..f57f919 100644 --- a/app/devices/api.py +++ b/app/devices/api.py @@ -11,6 +11,7 @@ from .models import (Device, AccessLevel) from itsdangerous import URLSafeSerializer from app.core import app +from app.errors import NotPresentError from app.jsonql import api as jsonql @@ -110,7 +111,7 @@ def get_device_recordings(device_id): :raises: ValueError if device does not exist """ if not Device.exists(id=device_id): - raise ValueError("Device with id %s does not exist" % device_id) + raise NotPresentError("Device with id %s does not exist" % device_id) return Recording.get_many(device_id=device_id) @@ -134,7 +135,7 @@ def get_device_recordings_filtered(device_id, record_type=None, :raises: ValueError if device does not exist """ if not Device.exists(id=device_id): - raise ValueError("Device with id %s does not exist" % device_id) + raise NotPresentError("Device with id %s does not exist" % device_id) return Recording.get_many_filtered(device_id, record_type, start_date, end_date) @@ -282,7 +283,7 @@ def create_recording_and_return(device_id, raw_json, :raises: ValueError if parsing fails or device does not exist """ if not Device.exists(id=device_id): - raise ValueError("Device does not exist!") + raise NotPresentError("Device does not exist!") if not authenticated: validate_hmac_in_message(device_id, raw_json) @@ -303,7 +304,7 @@ def create_recording(device_id, raw_json): :raises: ValueError if parsing fails or device does not exist """ if not Device.exists(id=device_id): - raise ValueError("Device does not exist!") + raise NotPresentError("Device does not exist!") validate_hmac_in_message(device_id, raw_json) recording = parse_raw_json_recording(device_id, raw_json) @@ -326,9 +327,9 @@ def create_targeted_device_sharing_token( :raises: ValueError if device does not exist """ if not Device.exists(id=device_id): - raise ValueError("Device does not exist!") + raise NotPresentError("Device does not exist!") if not AccessLevel.exists(id=access_level_id): - raise ValueError("AccessLevel does not exist!") + raise NotPresentError("AccessLevel does not exist!") data_to_serialize = { 'device_id': device_id, @@ -363,7 +364,7 @@ def activate_device_sharing_token(account_id, token): return False if not Device.exists(id=device_id): - raise ValueError("Device does not exist!") + raise NotPresentError("Device does not exist!") device_association = DeviceAssociation(device_id, account_id, access_level_id) @@ -376,7 +377,7 @@ def run_custom_query(device_id, request): Runs custom query as defined by jsonql module """ if not Device.exists(id=device_id): - raise ValueError("Device does not exist!") + raise NotPresentError("Device does not exist!") def recording_field_provider(name): if name == 'record_value': diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 0000000..df97152 --- /dev/null +++ b/app/errors.py @@ -0,0 +1,5 @@ +class NotPresentError(Exception): + pass + +class BadRequestError(Exception): + pass diff --git a/config.py b/config.py index e844357..83a8cb9 100644 --- a/config.py +++ b/config.py @@ -2,7 +2,7 @@ import os # App configuration DEBUG = os.environ['DEBUG'] -APP_VERSION = '0.4.1' +APP_VERSION = '0.4.2' # Define the application directory BASE_DIR = os.path.abspath(os.path.dirname(__file__))