Python sqlalchemy.exc.DataError() Examples

The following are 21 code examples of sqlalchemy.exc.DataError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.exc , or try the search function .
Example #1
Source File: inline_result_handler.py    From ultimate-poll-bot with MIT License 6 votes vote down vote up
def handle_chosen_inline_result(bot, update, session, user):
    """Save the chosen inline result."""
    result = update.chosen_inline_result
    poll_id = result.result_id

    try:
        poll = session.query(Poll).get(poll_id)

    except DataError:
        # Possile if the poll has been shared too often and
        # the inline result is picked despite saying otherwise.
        return

    try:
        reference = Reference(
            poll, ReferenceType.inline.name, inline_message_id=result.inline_message_id,
        )
        session.add(reference)
        session.commit()
    except UniqueViolation:
        # I don't know how this can happen, but it happens.
        return

    update_reference(session, bot, poll, reference)
    increase_user_stat(session, user, "inline_shares") 
Example #2
Source File: webservice.py    From pycopia with Apache License 2.0 6 votes vote down vote up
def related_add(modelname, entry_id, colname, relmodelname, rel_id):
    klass = get_model(modelname)
    relklass = get_model(relmodelname)
    metadata = models.get_column_metadata(klass, colname)
    # fetch parent and related objects
    dbrow = webhelpers.get_row(klass, entry_id)
    reldbrow = webhelpers.get_row(relklass, rel_id)
    # now add using appropriate semantics
    if metadata.uselist:
        col = getattr(dbrow, colname)
        col.append(reldbrow)
    else:
        setattr(dbrow, colname, reldbrow)
    try:
        webhelpers.dbsession.commit()
    except (DataError, IntegrityError), err:
        webhelpers.dbsession.rollback()
        raise 
Example #3
Source File: webservice.py    From pycopia with Apache License 2.0 6 votes vote down vote up
def related_remove(modelname, entry_id, colname, relmodelname, rel_id):
    klass = get_model(modelname)
    relklass = get_model(relmodelname)
    metadata = models.get_column_metadata(klass, colname)
    # fetch parent and related objects
    dbrow = webhelpers.get_row(klass, entry_id)
    if metadata.uselist:
        reldbrow = webhelpers.get_row(relklass, rel_id)
        col = getattr(dbrow, colname)
        col.remove(reldbrow)
    else:
        if metadata.nullable:
            setattr(dbrow, colname, None)
        else:
            raise DataError("Removing non-nullable relation")
    try:
        webhelpers.dbsession.commit()
    except (DataError, IntegrityError), err:
        webhelpers.dbsession.rollback()
        raise 
Example #4
Source File: test_model.py    From dokomoforms with GNU General Public License v3.0 6 votes vote down vote up
def test_integer_incorrect_range(self):
        """A decimal is not an integer"""
        with self.assertRaises(DataError):
            with self.session.begin():
                creator, survey = self._create_blank_survey()
                survey.nodes = [
                    models.construct_survey_node(
                        node=models.construct_node(
                            type_constraint='integer',
                            title={'English': 'node'},
                        ),
                        sub_surveys=[
                            models.SubSurvey(
                                buckets=[
                                    models.construct_bucket(
                                        bucket_type='integer',
                                        bucket='(1.3, 2.3]'
                                    ),
                                ],
                            ),
                        ],
                    ),
                ]
                self.session.add(creator) 
Example #5
Source File: errors.py    From notifications-api with MIT License 5 votes vote down vote up
def register_errors(blueprint):
    @blueprint.errorhandler(InvalidEmailError)
    def invalid_format(error):
        # Please not that InvalidEmailError is re-raised for InvalidEmail or InvalidPhone,
        # work should be done in the utils app to tidy up these errors.
        current_app.logger.info(error)
        return jsonify(status_code=400,
                       errors=[{"error": error.__class__.__name__, "message": str(error)}]), 400

    @blueprint.errorhandler(InvalidRequest)
    def invalid_data(error):
        current_app.logger.info(error)
        response = jsonify(error.to_dict_v2()), error.status_code
        return response

    @blueprint.errorhandler(JsonSchemaValidationError)
    def validation_error(error):
        current_app.logger.info(error)
        return jsonify(json.loads(error.message)), 400

    @blueprint.errorhandler(JobIncompleteError)
    def job_incomplete_error(error):
        return jsonify(error.to_dict_v2()), 500

    @blueprint.errorhandler(NoResultFound)
    @blueprint.errorhandler(DataError)
    def no_result_found(e):
        current_app.logger.info(e)
        return jsonify(status_code=404,
                       errors=[{"error": e.__class__.__name__, "message": "No result found"}]), 404

    @blueprint.errorhandler(AuthError)
    def auth_error(error):
        current_app.logger.info('API AuthError, client: {} error: {}'.format(request.headers.get('User-Agent'), error))
        return jsonify(error.to_dict_v2()), error.code

    @blueprint.errorhandler(Exception)
    def internal_server_error(error):
        current_app.logger.exception(error)
        return jsonify(status_code=500,
                       errors=[{"error": error.__class__.__name__, "message": 'Internal server error'}]), 500 
Example #6
Source File: test_model.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def test_exception_raised_with_invalid_state(session):
    """
    Test that we get an exception raised when we try
    to add a new row with an invalid state.
    """
    workflow_run_data = dict(
        workflow_name="DUMMY_WORKFLOW_NAME",
        parameters={"DUMMY_PARAM_NAME": "DUMMY_PARAM_VALUE"},
        state="INVALID_STATE",
    )
    with pytest.raises(DataError):
        WorkflowRuns.set_state(**workflow_run_data, session=session) 
Example #7
Source File: github_repo.py    From depsy with MIT License 5 votes vote down vote up
def commit_repo(repo):
    try:
        db.session.commit()
    except DataError:
        print "error committing repo, rolling back and setting save error for ", repo
        db.session.rollback()
        repo.set_save_error()
        safe_commit(db) 
Example #8
Source File: views.py    From planespotter with MIT License 5 votes vote down vote up
def catch_integrity_errors(session):
    """Returns a decorator that catches database integrity errors.

    `session` is the SQLAlchemy session in which all database transactions will
    be performed.

    View methods can be wrapped like this::

        @catch_integrity_errors(session)
        def get(self, *args, **kw):
            return '...'

    Specifically, functions wrapped with the returned decorator catch
    :exc:`IntegrityError`s, :exc:`DataError`s, and
    :exc:`ProgrammingError`s. After the exceptions are caught, the session is
    rolled back, the exception is logged on the current Flask application, and
    an error response is returned to the client.

    """
    def decorator(func):
        @wraps(func)
        def wrapped(*args, **kw):
            try:
                return func(*args, **kw)
            # TODO should `sqlalchemy.exc.InvalidRequestError`s also be caught?
            except (DataError, IntegrityError, ProgrammingError) as exception:
                session.rollback()
                current_app.logger.exception(str(exception))
                return dict(message=type(exception).__name__), 400
        return wrapped
    return decorator 
Example #9
Source File: test_model.py    From dokomoforms with GNU General Public License v3.0 5 votes vote down vote up
def test_reject_incorrect_answer_syntax(self):
        with self.session.begin():
            creator = models.Administrator(name='creator')
            survey = models.Survey(
                title={'English': 'survey'},
                nodes=[
                    models.construct_survey_node(
                        node=models.construct_node(
                            type_constraint='integer',
                            title={'English': 'integer question'},
                        ),
                    ),
                ],
            )
            creator.surveys = [survey]

            self.session.add(creator)

        with self.assertRaises(DataError):
            with self.session.begin():
                the_survey = self.session.query(models.Survey).one()
                submission = models.PublicSubmission(
                    survey=the_survey,
                    answers=[
                        models.construct_answer(
                            survey_node=the_survey.nodes[0],
                            type_constraint='integer',
                            answer='not an integer',
                        ),
                    ],
                )

                self.session.add(submission) 
Example #10
Source File: test_users_dao.py    From notifications-api with MIT License 5 votes vote down vote up
def test_get_user_invalid_id(notify_db_session):
    with pytest.raises(DataError):
        get_user_by_id(user_id="blah") 
Example #11
Source File: test_errors.py    From notifications-api with MIT License 5 votes vote down vote up
def test_data_errors(app_for_test):
    with app_for_test.test_request_context():
        with app_for_test.test_client() as client:
            response = client.get(url_for('v2_under_test.raising_data_error'))
            assert response.status_code == 404
            error = response.json
            assert error == {"status_code": 404,
                             "errors": [{"error": "DataError", "message": "No result found"}]} 
Example #12
Source File: base.py    From zou with GNU Affero General Public License v3.0 5 votes vote down vote up
def post(self):
        results = []
        self.sg_entries = request.json

        self.check_permissions()
        self.prepare_import()

        for sg_entry in self.filtered_entries():
            try:
                data = self.extract_data(sg_entry)
                result_entry = self.import_entry(data)
                results.append(result_entry)
            except ShotgunEntryImportFailed as exception:
                current_app.logger.warning(exception)
            except KeyError as exception:
                current_app.logger.warning(exception)
                current_app.logger.error(
                    "Your data is not properly formatted: %s" % sg_entry
                )
            except IntegrityError as exception:
                current_app.logger.error(exception)
                current_app.logger.error(
                    "Data information are duplicated or wrong: %s" % sg_entry
                )
                raise
            except DataError as exception:
                current_app.logger.error(exception)
                current_app.logger.error(
                    "Data cannot be stored (schema error)" % sg_entry
                )
                raise

        self.post_processing()

        return fields.serialize_models(results), 200 
Example #13
Source File: test_overview.py    From marcotti with MIT License 5 votes vote down vote up
def test_competition_name_overflow_error(session):
    """Competition 003: Verify error if competition name exceeds field length."""
    too_long_name = "leaguename" * 9
    record = mco.Competitions(name=unicode(too_long_name), level=2)
    with pytest.raises(DataError):
        session.add(record)
        session.commit() 
Example #14
Source File: test_overview.py    From marcotti with MIT License 5 votes vote down vote up
def test_country_code_error(session):
    too_long_code = "BOGUS"
    country = mco.Countries(name=unicode("Fredonia"), code=too_long_code,
                            confederation=enums.ConfederationType.south_america)
    with pytest.raises(DataError):
        session.add(country)
        session.commit() 
Example #15
Source File: test_overview.py    From marcotti with MIT License 5 votes vote down vote up
def test_country_name_overflow_error(session):
    """Country 003: Verify error if country name exceeds field length."""
    too_long_name = "blahblah" * 8
    too_long_country = mco.Countries(name=unicode(too_long_name), confederation=enums.ConfederationType.north_america)
    with pytest.raises(DataError):
        session.add(too_long_country)
        session.commit() 
Example #16
Source File: test_club.py    From marcotti with MIT License 5 votes vote down vote up
def test_club_name_overflow(session):
    too_long_name = "blahblah" * 8
    too_long_club = mc.Clubs(name=too_long_name,
                             country=mco.Countries(name=u"foo", confederation=enums.ConfederationType.fifa))
    with pytest.raises(DataError):
        session.add(too_long_club)
        session.commit() 
Example #17
Source File: tasks_service.py    From zou with GNU Affero General Public License v3.0 5 votes vote down vote up
def create_or_update_time_spent(task_id, person_id, date, duration, add=False):
    """
    Create a new time spent if it doesn't exist. If it exists, it update it
    with the new duratin and returns it from the database.
    """
    try:
        time_spent = TimeSpent.get_by(
            task_id=task_id, person_id=person_id, date=date
        )
    except DataError:
        raise WrongDateFormatException

    if time_spent is not None:
        if duration == 0:
            time_spent.delete()
        elif add:
            time_spent.update({"duration": time_spent.duration + duration})
        else:
            time_spent.update({"duration": duration})
        events.emit("time-spent:update", {"time_spent_id": str(time_spent.id)})
    else:
        time_spent = TimeSpent.create(
            task_id=task_id, person_id=person_id, date=date, duration=duration
        )
        events.emit("time-spent:new", {"time_spent_id": str(time_spent.id)})

    task = Task.get(task_id)
    task.duration = 0
    time_spents = TimeSpent.get_all_by(task_id=task_id)
    for time_spent in time_spents:
        task.duration += time_spent.duration
    task.save()
    clear_task_cache(task_id)
    events.emit("task:update", {"task_id": task_id})

    return time_spent.serialize() 
Example #18
Source File: time_spents_service.py    From zou with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_time_spents(person_id, date):
    """
    Return time spents for given person and date.
    """
    try:
        time_spents = TimeSpent.query.filter_by(
            person_id=person_id, date=date
        ).all()
    except DataError:
        raise WrongDateFormatException
    return fields.serialize_list(time_spents) 
Example #19
Source File: auth.py    From notifications-api with MIT License 4 votes vote down vote up
def requires_auth():
    request_helper.check_proxy_header_before_request()

    auth_token = get_auth_token(request)
    issuer = __get_token_issuer(auth_token)  # ie the `iss` claim which should be a service ID

    try:
        with AUTH_DB_CONNECTION_DURATION_SECONDS.time():
            service = SerialisedService.from_id(issuer)
    except DataError:
        raise AuthError("Invalid token: service id is not the right data type", 403)
    except NoResultFound:
        raise AuthError("Invalid token: service not found", 403)

    if not service.api_keys:
        raise AuthError("Invalid token: service has no API keys", 403, service_id=service.id)

    if not service.active:
        raise AuthError("Invalid token: service is archived", 403, service_id=service.id)

    for api_key in service.api_keys:
        try:
            decode_jwt_token(auth_token, api_key.secret)
        except TokenExpiredError:
            err_msg = "Error: Your system clock must be accurate to within 30 seconds"
            raise AuthError(err_msg, 403, service_id=service.id, api_key_id=api_key.id)
        except TokenAlgorithmError:
            err_msg = "Invalid token: algorithm used is not HS256"
            raise AuthError(err_msg, 403, service_id=service.id, api_key_id=api_key.id)
        except TokenDecodeError:
            # we attempted to validate the token but it failed meaning it was not signed using this api key.
            # Let's try the next one
            # TODO: Change this so it doesn't also catch `TokenIssuerError` or `TokenIssuedAtError` exceptions (which
            # are children of `TokenDecodeError`) as these should cause an auth error immediately rather than
            # continue on to check the next API key
            continue
        except TokenError:
            # General error when trying to decode and validate the token
            raise AuthError(GENERAL_TOKEN_ERROR_MESSAGE, 403, service_id=service.id, api_key_id=api_key.id)

        if api_key.expiry_date:
            raise AuthError("Invalid token: API key revoked", 403, service_id=service.id, api_key_id=api_key.id)

        g.service_id = service.id
        _request_ctx_stack.top.authenticated_service = service
        _request_ctx_stack.top.api_user = api_key

        current_app.logger.info('API authorised for service {} with api key {}, using issuer {} for URL: {}'.format(
            service.id,
            api_key.id,
            request.headers.get('User-Agent'),
            request.base_url
        ))
        return
    else:
        # service has API keys, but none matching the one the user provided
        raise AuthError("Invalid token: API key not found", 403, service_id=service.id) 
Example #20
Source File: test_errors.py    From notifications-api with MIT License 4 votes vote down vote up
def app_for_test():
    import flask
    from flask import Blueprint
    from app.authentication.auth import AuthError
    from app.v2.errors import BadRequestError, TooManyRequestsError, JobIncompleteError
    from app import init_app

    app = flask.Flask(__name__)
    app.config['TESTING'] = True
    init_app(app)
    from app import statsd_client
    statsd_client.init_app(app)

    from app.v2.errors import register_errors
    blue = Blueprint("v2_under_test", __name__, url_prefix='/v2/under_test')

    @blue.route("/raise_auth_error", methods=["GET"])
    def raising_auth_error():
        raise AuthError("some message", 403)

    @blue.route("/raise_bad_request", methods=["GET"])
    def raising_bad_request():
        raise BadRequestError(message="you forgot the thing")

    @blue.route("/raise_too_many_requests", methods=["GET"])
    def raising_too_many_requests():
        raise TooManyRequestsError(sending_limit="452")

    @blue.route("/raise_validation_error", methods=["GET"])
    def raising_validation_error():
        from app.schema_validation import validate
        from app.v2.notifications.notification_schemas import post_sms_request
        validate({"template_id": "bad_uuid"}, post_sms_request)

    @blue.route("raise_data_error", methods=["GET"])
    def raising_data_error():
        raise DataError("There was a db problem", "params", "orig")

    @blue.route("raise_job_incomplete_error", methods=["GET"])
    def raising_job_incomplete_error():
        raise JobIncompleteError("Raising job incomplete error")

    @blue.route("raise_exception", methods=["GET"])
    def raising_exception():
        raise AssertionError("Raising any old exception")

    register_errors(blue)
    app.register_blueprint(blue)

    return app 
Example #21
Source File: testcases.py    From pycopia with Apache License 2.0 4 votes vote down vote up
def post(self, request, tcid):
        dbrow = get_testcase(tcid)
        data = request.POST
        if data.get("completed") == "0": # yes
            if data.get("passfail") == "0": # yes
                result = webhelpers.PASSED
            else:
                result = webhelpers.FAILED
        else:
            result = webhelpers.INCOMPLETE

        username = request.session["username"]
        tester = models.User.get_by_username(webhelpers.dbsession, username)
        build=webhelpers.resolve_build(data.get("build"))

        rr = models.create(models.TestResult,
                objecttype=webhelpers.RUNNER,
                testcase=None,
                environment=webhelpers.resolve_environment(data.get("environment")),
                build=build,
                tester=tester,
                testversion=None,
                parent=None,
                starttime=data.get("starttime"),
                endtime=str(models.tables.time_now()),
                arguments=None,
                result=result,
                diagnostic=None,
                note=None,
                valid=True,
            )
        webhelpers.dbsession.add(rr)
        tr = models.create(models.TestResult,
                objecttype=webhelpers.TEST,
                testcase=dbrow,
                environment=None,
                build=build,
                tester=tester,
                testversion=None,
                parent=rr,
                starttime=data.get("starttime"),
                endtime=str(models.tables.time_now()),
                arguments=None,
                result=result,
                diagnostic=IF(result != webhelpers.PASSED, data.get("note")),
                note=IF(result == webhelpers.PASSED, data.get("note")),
                valid=True,
            )
        webhelpers.dbsession.add(tr)
        try:
            webhelpers.dbsession.commit()
        except (DataError, IntegrityError), err:
            webhelpers.dbsession.rollback()
            title = "Test runner error"
            resp = framework.ResponseDocument(request, testcase_run_constructor, title=title)
            resp.doc.new_para(err, class_="error")
            resp.doc.new_para("There was an error recording this test. Please try again.")
            return resp.finalize()