50
50
import sentry_sdk
51
51
from sentry_sdk ._compat import PY2 , PY33 , PY37 , implements_str , text_type , urlparse
52
52
from sentry_sdk ._types import TYPE_CHECKING
53
+ from sentry_sdk .consts import DEFAULT_MAX_VALUE_LENGTH
53
54
54
55
if TYPE_CHECKING :
55
56
from types import FrameType , TracebackType
75
76
# The logger is created here but initialized in the debug support module
76
77
logger = logging .getLogger ("sentry_sdk.errors" )
77
78
78
- MAX_STRING_LENGTH = 1024
79
+
79
80
BASE64_ALPHABET = re .compile (r"^[a-zA-Z0-9/+=]*$" )
80
81
81
82
SENSITIVE_DATA_SUBSTITUTE = "[Filtered]"
@@ -468,6 +469,7 @@ def iter_stacks(tb):
468
469
def get_lines_from_file (
469
470
filename , # type: str
470
471
lineno , # type: int
472
+ max_length = None , # type: Optional[int]
471
473
loader = None , # type: Optional[Any]
472
474
module = None , # type: Optional[str]
473
475
):
@@ -496,11 +498,12 @@ def get_lines_from_file(
496
498
497
499
try :
498
500
pre_context = [
499
- strip_string (line .strip ("\r \n " )) for line in source [lower_bound :lineno ]
501
+ strip_string (line .strip ("\r \n " ), max_length = max_length )
502
+ for line in source [lower_bound :lineno ]
500
503
]
501
- context_line = strip_string (source [lineno ].strip ("\r \n " ))
504
+ context_line = strip_string (source [lineno ].strip ("\r \n " ), max_length = max_length )
502
505
post_context = [
503
- strip_string (line .strip ("\r \n " ))
506
+ strip_string (line .strip ("\r \n " ), max_length = max_length )
504
507
for line in source [(lineno + 1 ) : upper_bound ]
505
508
]
506
509
return pre_context , context_line , post_context
@@ -512,6 +515,7 @@ def get_lines_from_file(
512
515
def get_source_context (
513
516
frame , # type: FrameType
514
517
tb_lineno , # type: int
518
+ max_value_length = None , # type: Optional[int]
515
519
):
516
520
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
517
521
try :
@@ -528,7 +532,9 @@ def get_source_context(
528
532
loader = None
529
533
lineno = tb_lineno - 1
530
534
if lineno is not None and abs_path :
531
- return get_lines_from_file (abs_path , lineno , loader , module )
535
+ return get_lines_from_file (
536
+ abs_path , lineno , max_value_length , loader = loader , module = module
537
+ )
532
538
return [], None , []
533
539
534
540
@@ -602,9 +608,13 @@ def filename_for_module(module, abs_path):
602
608
603
609
604
610
def serialize_frame (
605
- frame , tb_lineno = None , include_local_variables = True , include_source_context = True
611
+ frame ,
612
+ tb_lineno = None ,
613
+ include_local_variables = True ,
614
+ include_source_context = True ,
615
+ max_value_length = None ,
606
616
):
607
- # type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any]
617
+ # type: (FrameType, Optional[int], bool, bool, Optional[int] ) -> Dict[str, Any]
608
618
f_code = getattr (frame , "f_code" , None )
609
619
if not f_code :
610
620
abs_path = None
@@ -630,7 +640,7 @@ def serialize_frame(
630
640
631
641
if include_source_context :
632
642
rv ["pre_context" ], rv ["context_line" ], rv ["post_context" ] = get_source_context (
633
- frame , tb_lineno
643
+ frame , tb_lineno , max_value_length
634
644
)
635
645
636
646
if include_local_variables :
@@ -639,8 +649,12 @@ def serialize_frame(
639
649
return rv
640
650
641
651
642
- def current_stacktrace (include_local_variables = True , include_source_context = True ):
643
- # type: (bool, bool) -> Any
652
+ def current_stacktrace (
653
+ include_local_variables = True , # type: bool
654
+ include_source_context = True , # type: bool
655
+ max_value_length = None , # type: Optional[int]
656
+ ):
657
+ # type: (...) -> Dict[str, Any]
644
658
__tracebackhide__ = True
645
659
frames = []
646
660
@@ -652,6 +666,7 @@ def current_stacktrace(include_local_variables=True, include_source_context=True
652
666
f ,
653
667
include_local_variables = include_local_variables ,
654
668
include_source_context = include_source_context ,
669
+ max_value_length = max_value_length ,
655
670
)
656
671
)
657
672
f = f .f_back
@@ -724,16 +739,19 @@ def single_exception_from_error_tuple(
724
739
if client_options is None :
725
740
include_local_variables = True
726
741
include_source_context = True
742
+ max_value_length = DEFAULT_MAX_VALUE_LENGTH # fallback
727
743
else :
728
744
include_local_variables = client_options ["include_local_variables" ]
729
745
include_source_context = client_options ["include_source_context" ]
746
+ max_value_length = client_options ["max_value_length" ]
730
747
731
748
frames = [
732
749
serialize_frame (
733
750
tb .tb_frame ,
734
751
tb_lineno = tb .tb_lineno ,
735
752
include_local_variables = include_local_variables ,
736
753
include_source_context = include_source_context ,
754
+ max_value_length = max_value_length ,
737
755
)
738
756
for tb in iter_stacks (tb )
739
757
]
@@ -819,9 +837,7 @@ def exceptions_from_error(
819
837
parent_id = exception_id
820
838
exception_id += 1
821
839
822
- should_supress_context = (
823
- hasattr (exc_value , "__suppress_context__" ) and exc_value .__suppress_context__ # type: ignore
824
- )
840
+ should_supress_context = hasattr (exc_value , "__suppress_context__" ) and exc_value .__suppress_context__ # type: ignore
825
841
if should_supress_context :
826
842
# Add direct cause.
827
843
# The field `__cause__` is set when raised with the exception (using the `from` keyword).
@@ -1082,13 +1098,11 @@ def _is_in_project_root(abs_path, project_root):
1082
1098
1083
1099
def strip_string (value , max_length = None ):
1084
1100
# type: (str, Optional[int]) -> Union[AnnotatedValue, str]
1085
- # TODO: read max_length from config
1086
1101
if not value :
1087
1102
return value
1088
1103
1089
1104
if max_length is None :
1090
- # This is intentionally not just the default such that one can patch `MAX_STRING_LENGTH` and affect `strip_string`.
1091
- max_length = MAX_STRING_LENGTH
1105
+ max_length = DEFAULT_MAX_VALUE_LENGTH
1092
1106
1093
1107
length = len (value .encode ("utf-8" ))
1094
1108
0 commit comments