Merged in enhancement/errors (pull request #54)

Enhancement/errors
develop
Ensar Sarajcic 2018-11-03 17:25:36 +00:00
commit 78dc506586
5 changed files with 36 additions and 27 deletions

View File

@ -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()

View File

@ -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):

View File

@ -86,7 +86,6 @@ 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'],
@ -102,8 +101,6 @@ class AccountListResource(Resource):
'ETF IoT Email confirmation',
html)
return UserSchema().dump(created_account), 201
except ValueError:
abort(422, message='Account already exists', status='error')
class AccountEmailTokenResource(Resource):

View File

@ -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':

5
app/errors.py 100644
View File

@ -0,0 +1,5 @@
class NotPresentError(Exception):
pass
class BadRequestError(Exception):
pass