Ejemplo n.º 1
0
async def validation_exception_handler(request: Request,
                                       exc: RequestValidationError):
    logger.exception("Validation error occurred, %s", str(exc))
    return JSONResponse(
        status_code=422,
        content={"message": "Validation error"},
    )
Ejemplo n.º 2
0
async def database_exception_handler(request: Request,
                                     exc: database.DatabaseError):
    logger.exception("Error with the database, %s", str(exc))
    return JSONResponse(
        status_code=500,
        content={"message": "Internal server error"},
    )
Ejemplo n.º 3
0
async def _get_json(url: str):
    async with aiohttp.ClientSession() as ses:
        async with ses.get(url) as resp:
            try:
                json = await resp.json()
            except Exception as e:
                logger.exception(e)
                return {}
            return json
Ejemplo n.º 4
0
def session() -> Iterator[sqlite3.Connection]:
    conn = sqlite3.connect('../eng.db')
    try:
        yield conn
        conn.commit()
    except Exception as e:
        logger.exception(e)
        raise
    finally:
        conn.close()
Ejemplo n.º 5
0
async def session(**kwargs) -> AsyncIterator[AsyncSession]:
    new_ses = AsyncSession(bind=engine, expire_on_commit=False, **kwargs)
    try:
        yield new_ses
        await new_ses.commit()
    except Exception as e:
        await new_ses.rollback()
        logger.exception(e)
        raise DatabaseError(e) from e
    finally:
        await new_ses.close()
Ejemplo n.º 6
0
async def post_view(request: Request, word: str = Body(...)):
    word = word.split('=')[-1]
    corpus_examples, linked_words = {}, {}

    try:
        corpus_examples = await example_routes.get_corpus_examples(word, pages_count=5)
    except Exception:
        logger.exception("Error getting corpus examples")
    try:
        linked_words = await word_routes.get_link_words(word)
    except Exception:
        logger.exception("Error getting linked words")

    context = {
        'request': request,
        'word': word,
        'corpus_examples': corpus_examples.get('examples', []),
        'linked_words': linked_words.get('synonyms', [])
    }

    return templates.TemplateResponse('view.html', context)