<commit_before># -*- coding: utf-8 -*-
# http://google-styleguide.googlecode.com/svn/trunk/pyguide.

import traceback
from jsonschema import ValidationError
from smsgw.lib.utils import response


class ErrorResource(Exception):
    status_code = 400

    def __init__(self, status_code=None, payload=None, message=None):
        Exception.__init__(self)
        self.message = message
        if status_code is not None:
            self.status_code = status_code

    @staticmethod
    def register(app, **kwargs):

        @app.errorhandler(404)
        def on_unauthorized(error):
            return response(None, status_code=401, message="Unauthorized.")

        @app.errorhandler(404)
        def on_page_not_found(error):
            return response(None, status_code=404, message="Not found.")

        @app.errorhandler(ErrorResource)
        def on_error_resource(error):
            """ Error handler for ErrorResource exception """
            return response(None, error.status_code, error.message)

        @app.errorhandler(ValidationError)
        def on_validation_error(error):
            message = error.schema.get('message') or error.message
            return response(None, status_code=400, message=message)<commit_msg>ErrorResource: adding exception handler<commit_after># -*- coding: utf-8 -*-
# http://google-styleguide.googlecode.com/svn/trunk/pyguide.

import traceback
from jsonschema import ValidationError
from smsgw.lib.utils import response
from flask import current_app


class ErrorResource(Exception):
    status_code = 400

    def __init__(self, status_code=None, payload=None, message=None):
        Exception.__init__(self)
        self.message = message
        if status_code is not None:
            self.status_code = status_code

    @staticmethod
    def register(app, **kwargs):

        @app.errorhandler(404)
        def on_unauthorized(error):
            return response(None, status_code=401, message="Unauthorized.")

        @app.errorhandler(404)
        def on_page_not_found(error):
            return response(None, status_code=404, message="Not found.")

        @app.errorhandler(ErrorResource)
        def on_error_resource(error):
            """ Error handler for ErrorResource exception """
            return response(None, error.status_code, error.message)

        @app.errorhandler(ValidationError)
        def on_validation_error(error):
            message = error.schema.get('message') or error.message
            return response(None, status_code=400, message=message)

        @app.errorhandler(Exception)
        def on_unhandled_exception(error):
            """ Error handler for unhandled exceptions """
            current_app.logger.error(traceback.format_exc())            

            return response(None, status_code=500)
{'fraction_non_alphanumeric': 0.09796936230851443, 'fraction_numerical': 0.01460634128963306, 'mean_word_length': 2.943820224719101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7292871', 'n_tokens_mistral': 797, 'n_tokens_neox': 750, 'n_words': 168}