Files
Zamil Majdyandmajdyz f2dbfa7e7c [AGPT-621] Moving on with code generation with a buggy code (Ruff - PyRight failure) (#181)
To prevent frustrated tester, we should let buggy code to be generated instead of failing the project generation (if it failed on the linter) by appending a # TODO line on the code.

---------

Co-authored-by: majdyz <[email protected]>
2024-04-04 17:38:06 +02:00

79 lines
2.0 KiB
Python

import json
import logging
import traceback
from typing import Callable
from fastapi.responses import JSONResponse
from prisma.enums import DevelopmentPhase, Status
from prisma.errors import RecordNotFoundError
from prisma.models import EventLog
from codex.api_model import Identifiers
logger = logging.getLogger(__name__)
# This is a custom logger that prints the exception stack trace on error by default
class Logger(logging.Logger):
def __init__(self, name):
super().__init__(name)
def error(self, msg, *args, **kwargs):
if not kwargs.get("exc_info"):
kwargs["exc_info"] = True
super().error(msg, *args, **kwargs)
async def execute_and_log(
id: Identifiers,
step: DevelopmentPhase,
event: str,
key: str,
func: Callable,
*args,
**kwargs,
):
try:
await log_event(id, step, event, key, Status.STARTED)
result = await func(*args, **kwargs)
await log_event(id, step, event, key, Status.SUCCESS)
return result
except Exception as e:
error = "\n".join(traceback.format_exception(e)).split(
"Traceback (most recent call last):"
)[-1]
logger.error(f"Error during {step} {event}: {error}")
await log_event(id, step, event, key, Status.FAILED, error)
status_code = (
404
if isinstance(e, RecordNotFoundError) or isinstance(e, FileNotFoundError)
else 500
)
return JSONResponse(
content=json.dumps({"error": str(e)}),
status_code=status_code,
)
async def log_event(
id: Identifiers,
step: DevelopmentPhase,
event: str,
key: str,
status: Status = Status.FAILED,
data: str | None = None,
):
await EventLog.prisma().create(
data={
"userId": id.user_id,
"applicationId": id.app_id,
"event": event,
"keyType": step,
"keyValue": key,
"status": status,
"message": data,
}
)