Skip to content

Fix redirect function #19

Description

@bramz

Bug report

If the row is referenced before assignment an error is thrown.

Traceback

  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 390, in start
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 366, in _handle
    resp = await handler(request)
  File "pfurl/__main__.py", line 174, in hash_redirect
    return aiohttp.web.HTTPFound(row[0])
UnboundLocalError: local variable 'row' referenced before assignment

Resolution

Rewrite to ensure the row is not referenced before assignment in the following function.

async def hash_redirect(request):
    statement = 'select url from pfurl where hash=%s'
    hash = request.match_info.get('hash')
    ret = []
    
    async with request.app['pool'].acquire() as connection:
        async with connection.cursor() as cursor:
            await cursor.execute(
                statement,
                (hash,)
            )
            async for row in cursor:
                ret.append(row)

    return aiohttp.web.HTTPFound(row[0])

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions