fix: ensure correct context in sys.exc_info

This commit is contained in:
Sagar Vora 2025-02-19 17:33:53 +05:30
parent a257a6a43e
commit 60b889c3b8
3 changed files with 27 additions and 17 deletions

View file

@ -324,9 +324,8 @@ def make_form_dict(request: Request):
frappe.throw(_("Invalid request arguments"))
@handle_does_not_exist_error
def handle_exception(e):
e = handle_does_not_exist_error(e)
response = None
http_status_code = getattr(e, "http_status_code", 500)
accept_header = frappe.get_request_header("Accept") or ""

View file

@ -853,12 +853,20 @@ def check_doctype_permission(doctype: str, ptype: str = "read") -> None:
frappe.local.message_log = _message_log
def handle_does_not_exist_error(e: Exception) -> Exception:
if isinstance(e, frappe.DoesNotExistError) and (doctype := getattr(e, "doctype", None)):
try:
check_doctype_permission(doctype)
def handle_does_not_exist_error(fn):
"""
Decorator to override DoesNotExistError when handling exceptions.
Requires the first argument to be an Exception.
"""
except frappe.PermissionError as _e:
return _e
@functools.wraps(fn)
def wrapper(e, *args, **kwargs):
if isinstance(e, frappe.DoesNotExistError) and (doctype := getattr(e, "doctype", None)):
try:
check_doctype_permission(doctype)
except frappe.PermissionError as _e:
return fn(_e, *args, **kwargs)
return e
return fn(e, *args, **kwargs)
return wrapper

View file

@ -20,18 +20,21 @@ def get_response(path=None, http_status_code=200) -> Response:
return renderer_instance.render()
except Exception as e:
e = handle_does_not_exist_error(e)
return handle_exception(e, endpoint, path, http_status_code)
if isinstance(e, frappe.Redirect):
return RedirectPage(endpoint or path, e.http_status_code).render()
if isinstance(e, frappe.PermissionError):
return NotPermittedPage(endpoint, http_status_code, exception=e).render()
@handle_does_not_exist_error
def handle_exception(e, endpoint, path, http_status_code):
if isinstance(e, frappe.Redirect):
return RedirectPage(endpoint or path, e.http_status_code).render()
if isinstance(e, frappe.PageDoesNotExistError):
return NotFoundPage(endpoint, http_status_code).render()
if isinstance(e, frappe.PermissionError):
return NotPermittedPage(endpoint, http_status_code, exception=e).render()
return ErrorPage(exception=e).render()
if isinstance(e, frappe.PageDoesNotExistError):
return NotFoundPage(endpoint, http_status_code).render()
return ErrorPage(exception=e).render()
def get_response_content(path=None, http_status_code=200) -> str: