fix: ensure correct context in sys.exc_info
This commit is contained in:
parent
a257a6a43e
commit
60b889c3b8
3 changed files with 27 additions and 17 deletions
|
|
@ -324,9 +324,8 @@ def make_form_dict(request: Request):
|
||||||
frappe.throw(_("Invalid request arguments"))
|
frappe.throw(_("Invalid request arguments"))
|
||||||
|
|
||||||
|
|
||||||
|
@handle_does_not_exist_error
|
||||||
def handle_exception(e):
|
def handle_exception(e):
|
||||||
e = handle_does_not_exist_error(e)
|
|
||||||
|
|
||||||
response = None
|
response = None
|
||||||
http_status_code = getattr(e, "http_status_code", 500)
|
http_status_code = getattr(e, "http_status_code", 500)
|
||||||
accept_header = frappe.get_request_header("Accept") or ""
|
accept_header = frappe.get_request_header("Accept") or ""
|
||||||
|
|
|
||||||
|
|
@ -853,12 +853,20 @@ def check_doctype_permission(doctype: str, ptype: str = "read") -> None:
|
||||||
frappe.local.message_log = _message_log
|
frappe.local.message_log = _message_log
|
||||||
|
|
||||||
|
|
||||||
def handle_does_not_exist_error(e: Exception) -> Exception:
|
def handle_does_not_exist_error(fn):
|
||||||
if isinstance(e, frappe.DoesNotExistError) and (doctype := getattr(e, "doctype", None)):
|
"""
|
||||||
try:
|
Decorator to override DoesNotExistError when handling exceptions.
|
||||||
check_doctype_permission(doctype)
|
Requires the first argument to be an Exception.
|
||||||
|
"""
|
||||||
|
|
||||||
except frappe.PermissionError as _e:
|
@functools.wraps(fn)
|
||||||
return _e
|
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
|
||||||
|
|
|
||||||
|
|
@ -20,18 +20,21 @@ def get_response(path=None, http_status_code=200) -> Response:
|
||||||
return renderer_instance.render()
|
return renderer_instance.render()
|
||||||
|
|
||||||
except Exception as e:
|
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):
|
@handle_does_not_exist_error
|
||||||
return NotPermittedPage(endpoint, http_status_code, exception=e).render()
|
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):
|
if isinstance(e, frappe.PermissionError):
|
||||||
return NotFoundPage(endpoint, http_status_code).render()
|
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:
|
def get_response_content(path=None, http_status_code=200) -> str:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue