-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UserError exception type to separate server vs user errors #241
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
bots/migrations/0054_savedrun_finish_reason_alter_savedrun_example_id_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 4.2.7 on 2023-12-30 00:52 | ||
|
||
from django.db import migrations, models | ||
from bots.models import FinishReason | ||
|
||
|
||
def forwards_func(apps, schema_editor): | ||
saved_run = apps.get_model("bots", "SavedRun") | ||
saved_run.objects.filter(run_status="", error_msg="").update( | ||
finish_reason=FinishReason.DONE, | ||
) | ||
saved_run.objects.exclude(error_msg="").update( | ||
finish_reason=FinishReason.SERVER_ERROR, | ||
) | ||
|
||
|
||
def backwards_func(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("bots", "0053_alter_publishedrun_workflow_alter_savedrun_workflow_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="savedrun", | ||
name="finish_reason", | ||
field=models.IntegerField( | ||
choices=[(1, "User Error"), (2, "Server Error"), (3, "Done")], | ||
default=None, | ||
null=True, | ||
), | ||
), | ||
migrations.RunPython(forwards_func, backwards_func), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,16 @@ | |
from types import SimpleNamespace | ||
|
||
import sentry_sdk | ||
import pydantic | ||
|
||
import gooey_ui as st | ||
from app_users.models import AppUser | ||
from bots.models import SavedRun | ||
from bots.models import SavedRun, FinishReason | ||
from celeryapp.celeryconfig import app | ||
from daras_ai.image_input import truncate_text_words | ||
from daras_ai_v2 import settings | ||
from daras_ai_v2.base import StateKeys, err_msg_for_exc, BasePage | ||
from daras_ai_v2.base import BasePage, StateKeys, err_msg_for_exc | ||
from daras_ai_v2.exceptions import UserError, raise_as_user_error | ||
from daras_ai_v2.send_email import send_email_via_postmark | ||
from daras_ai_v2.settings import templates | ||
from gooey_ui.pubsub import realtime_push | ||
|
@@ -27,15 +29,16 @@ def gui_runner( | |
uid: str, | ||
state: dict, | ||
channel: str, | ||
query_params: dict = None, | ||
query_params: dict | None = None, | ||
is_api_call: bool = False, | ||
): | ||
page = page_cls(request=SimpleNamespace(user=AppUser.objects.get(id=user_id))) | ||
sr = page.run_doc_sr(run_id, uid) | ||
|
||
st.set_session_state(state) | ||
run_time = 0 | ||
run_time = 0.0 | ||
yield_val = None | ||
finish_reason = None | ||
error_msg = None | ||
set_query_params(query_params or {}) | ||
|
||
|
@@ -53,6 +56,7 @@ def save(done=False): | |
# set run status and run time | ||
status = { | ||
StateKeys.run_time: run_time, | ||
StateKeys.finish_reason: finish_reason, | ||
StateKeys.error_msg: error_msg, | ||
StateKeys.run_status: run_status, | ||
} | ||
|
@@ -75,30 +79,36 @@ def save(done=False): | |
try: | ||
gen = page.run(st.session_state) | ||
save() | ||
while True: | ||
# record time | ||
start_time = time() | ||
try: | ||
# advance the generator (to further progress of run()) | ||
yield_val = next(gen) | ||
# increment total time taken after every iteration | ||
run_time += time() - start_time | ||
continue | ||
# run completed | ||
except StopIteration: | ||
run_time += time() - start_time | ||
sr.transaction, sr.price = page.deduct_credits(st.session_state) | ||
break | ||
start_time = time() | ||
try: | ||
with raise_as_user_error([pydantic.ValidationError]): | ||
for yield_val in gen: | ||
# increment total time taken after every iteration | ||
run_time += time() - start_time | ||
save() | ||
except UserError as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. except UserError,pydantic.ValidationError |
||
# handled errors caused due to user input | ||
run_time += time() - start_time | ||
finish_reason = FinishReason.USER_ERROR | ||
traceback.print_exc() | ||
sentry_sdk.capture_exception(e) | ||
error_msg = err_msg_for_exc(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just do error_msg = str(e) |
||
except Exception as e: | ||
# render errors nicely | ||
except Exception as e: | ||
run_time += time() - start_time | ||
traceback.print_exc() | ||
sentry_sdk.capture_exception(e) | ||
error_msg = err_msg_for_exc(e) | ||
break | ||
finally: | ||
save() | ||
run_time += time() - start_time | ||
finish_reason = FinishReason.SERVER_ERROR | ||
traceback.print_exc() | ||
sentry_sdk.capture_exception(e) | ||
error_msg = err_msg_for_exc(e) | ||
else: | ||
# run completed | ||
run_time += time() - start_time | ||
finish_reason = FinishReason.DONE | ||
sr.transaction, sr.price = page.deduct_credits(st.session_state) | ||
finally: | ||
if not finish_reason: | ||
finish_reason = FinishReason.SERVER_ERROR | ||
error_msg = "Something went wrong. Please try again later." | ||
save(done=True) | ||
if not is_api_call: | ||
send_email_on_completion(page, sr) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from contextlib import contextmanager | ||
from typing import Type | ||
|
||
|
||
class UserError(Exception): | ||
pass | ||
|
||
|
||
@contextmanager | ||
def raise_as_user_error(excs: list[Type[Exception]]): | ||
try: | ||
yield | ||
except tuple(excs) as e: | ||
raise UserError(str(e)) from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either change this to be ErrorReason, or save RecipeRunState in db