from flask import jsonify

from . import main
from ..models import ValidationError


@main.app_errorhandler(ValidationError)
def validatation_error(e):
    return jsonify(error=e.message), 400


def generic_error_handler(e):
    # TODO: log the error
    headers = []
    error = e.description
    if e.code == 401:
        headers = [('WWW-Authenticate', 'Bearer')]
    elif e.code == 500:
        error = "Internal error"

    return jsonify(error=error),  e.code, headers


for code in range(400, 599):
    main.app_errorhandler(code)(generic_error_handler)

@@ -226,80 +226,120 @@
     
-# TODO: log the error%0A    headers = %5B%5D%0A    error = e.description
+headers = %5B%5D%0A    code = getattr(e, 'code', 500)%0A    error = getattr(e, 'description', 'Internal error')%0A
 %0A    if 
 e.co
@@ -334,18 +334,16 @@
 %0A    if 
-e.
 code == 
@@ -407,18 +407,16 @@
    elif 
-e.
 code == 
@@ -454,16 +454,16 @@
 error%22%0A%0A
+
     retu
@@ -491,11 +491,8 @@
 r), 
- e.
 code

Fix app error handler raising an attribute error