Skip to content

Commit

Permalink
fix(DX): Wrap print format errors (frappe#21944)
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
ankush authored Aug 6, 2023
1 parent 5fce1a5 commit a2b2998
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions frappe/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ class SessionBootFailed(ValidationError):
http_status_code = 500


class PrintFormatError(ValidationError):
pass


class TooManyWritesError(Exception):
pass

Expand Down
2 changes: 1 addition & 1 deletion frappe/utils/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,5 @@ def guess_exception_source(exception: str) -> str | None:
app_name = matches.group("app_name")
apps[app_name] += app_priority.get(app_name, 0)

if probably_source := apps.most_common(1):
if (probably_source := apps.most_common(1)) and probably_source[0][0] != "frappe":
return f"{probably_source[0][0]} (app)"
26 changes: 25 additions & 1 deletion frappe/utils/pdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import contextlib
import io
import os
import re
Expand Down Expand Up @@ -43,7 +44,30 @@ def pdf_header_html(soup, head, content, styles, html_id, css):


def pdf_body_html(template, args, **kwargs):
return template.render(args, filters={"len": len})
try:
return template.render(args, filters={"len": len})
except Exception as e:
# Guess line number ?
frappe.throw(
_("Error in print format on line {0}: {1}").format(
_guess_template_error_line_number(template), e
),
exc=frappe.PrintFormatError,
title=_("Print Format Error"),
)


def _guess_template_error_line_number(template) -> int | None:
"""Guess line on which exception occured from current traceback."""
with contextlib.suppress(Exception):
import sys
import traceback

_, _, tb = sys.exc_info()

for frame in reversed(traceback.extract_tb(tb)):
if template.filename in frame.filename:
return frame.lineno


def pdf_footer_html(soup, head, content, styles, html_id, css):
Expand Down

0 comments on commit a2b2998

Please sign in to comment.