How to use the mercadopago.errors function in mercadopago

To help you get started, we’ve selected a few mercadopago examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github federicobond / pymercadopago / tests / test_client.py View on Github external
res = expect(c, 'GET', '/payments')
    res.status_code = 401

    with pytest.raises(errors.AuthenticationError):
        c.get('/payments')

    res = expect(c, 'GET', '/payments')
    res.status_code = 404

    with pytest.raises(errors.NotFoundError):
        c.get('/payments')

    res = expect(c, 'GET', '/payments')
    res.status_code = 500

    with pytest.raises(errors.Error):
        c.get('/payments')
github federicobond / pymercadopago / tests / test_api.py View on Github external
def test_preapprovals(c):
    c.force_authenticate()

    res = expect(c, 'GET', '/preapproval/search', params={'id': '1234'})
    res.data = {'paging': {}, 'results': [{'id': '1234'}]}
    c.preapprovals.get('1234')

    # test not found for .get explicitly since this is a convenience method
    res = expect(c, 'GET', '/preapproval/search', params={'id': '1234'})
    res.data = {'paging': {}, 'results': []}
    with pytest.raises(errors.NotFoundError):
        c.preapprovals.get('1234')

    expect(c, 'POST', '/preapproval', json={'foo': 'bar'})
    c.preapprovals.create(foo='bar')

    expect(c, 'PUT', '/preapproval/1234', json={'foo': 'bar'})
    c.preapprovals.update(id='1234', foo='bar')

    expect(c, 'PUT', '/preapproval/1234', json={'status': 'cancelled'})
    c.preapprovals.cancel('1234')

    expect(c, 'PUT', '/preapproval/1234', json={'status': 'paused'})
    c.preapprovals.pause('1234')

    expect(c, 'GET', '/preapproval/search', params={'foo': 'bar'})
    c.preapprovals.search(foo='bar')
github federicobond / pymercadopago / tests / test_client.py View on Github external
def test_error_handling(c):
    c.force_authenticate()

    res = expect(c, 'GET', '/payments')
    res.status_code = 400

    with pytest.raises(errors.BadRequestError):
        c.get('/payments')

    res = expect(c, 'GET', '/payments')
    res.status_code = 401

    with pytest.raises(errors.AuthenticationError):
        c.get('/payments')

    res = expect(c, 'GET', '/payments')
    res.status_code = 404

    with pytest.raises(errors.NotFoundError):
        c.get('/payments')

    res = expect(c, 'GET', '/payments')
    res.status_code = 500
github federicobond / pymercadopago / tests / test_api.py View on Github external
def test_authentication_error(c):
    res = expect(c, 'POST', '/oauth/token', data={
        'client_id': 'XXX',
        'client_secret': 'XXX',
        'grant_type': 'client_credentials'
    })
    res.status_code = 400

    with pytest.raises(errors.BadRequestError):
        c.authenticate()
github federicobond / pymercadopago / mercadopago / client.py View on Github external
def _handle_request_error(self, error):
        if isinstance(error, requests.HTTPError):
            status = error.response.status_code

            if status == 400:
                raise errors.BadRequestError(error)
            elif status == 401:
                raise errors.AuthenticationError(error)
            elif status == 404:
                raise errors.NotFoundError(error)

        raise errors.Error(error)
github federicobond / pymercadopago / mercadopago / api.py View on Github external
def get(self, id):
        # NOTE: this is actually performing a search with ID and mangling
        # the response data to conform to a single object format.
        # There is no method to retrieve a preapproval by ID at the moment.

        res = self.search(id=id)

        if not res.data['results']:
            raise errors.NotFoundError('could not find preapproval with ID = %s' % id)

        res = Response(self._client.client, res._response)  # pylint: disable=protected-access
        res.data = res.data['results'][0]

        return res
github federicobond / pymercadopago / mercadopago / client.py View on Github external
def _handle_request_error(self, error):
        if isinstance(error, requests.HTTPError):
            status = error.response.status_code

            if status == 400:
                raise errors.BadRequestError(error)
            elif status == 401:
                raise errors.AuthenticationError(error)
            elif status == 404:
                raise errors.NotFoundError(error)

        raise errors.Error(error)