|
1 | 1 | from collections.abc import Sequence |
2 | | -from typing import Optional |
3 | 2 |
|
4 | | -from dagster._serdes import serialize_value |
5 | 3 | from dagster._utils.error import SerializableErrorInfo |
6 | 4 |
|
7 | | -ERROR_CLASS_NAME_SIZE_LIMIT = 1000 |
8 | | - |
9 | | - |
10 | | -def unwrap_user_code_error(error_info: SerializableErrorInfo) -> SerializableErrorInfo: |
11 | | - """Extracts the underlying error from the passed error, if it is a DagsterUserCodeLoadError.""" |
12 | | - if error_info.cls_name == "DagsterUserCodeLoadError": |
13 | | - return unwrap_user_code_error(error_info.cause) |
14 | | - return error_info |
15 | | - |
16 | | - |
17 | | -def truncate_serialized_error( |
18 | | - error_info: SerializableErrorInfo, |
19 | | - field_size_limit: int, |
20 | | - max_depth: int, |
21 | | - truncations: Optional[list[str]] = None, |
22 | | -): |
23 | | - truncations = [] if truncations is None else truncations |
24 | | - |
25 | | - if error_info.cause: |
26 | | - if max_depth == 0: |
27 | | - truncations.append("cause") |
28 | | - new_cause = ( |
29 | | - error_info.cause |
30 | | - if len(serialize_value(error_info.cause)) <= field_size_limit |
31 | | - else SerializableErrorInfo( |
32 | | - message="(Cause truncated due to size limitations)", |
33 | | - stack=[], |
34 | | - cls_name=None, |
35 | | - ) |
36 | | - ) |
37 | | - else: |
38 | | - new_cause = truncate_serialized_error( |
39 | | - error_info.cause, |
40 | | - field_size_limit, |
41 | | - max_depth=max_depth - 1, |
42 | | - truncations=truncations, |
43 | | - ) |
44 | | - error_info = error_info._replace(cause=new_cause) |
45 | | - |
46 | | - if error_info.context: |
47 | | - if max_depth == 0: |
48 | | - truncations.append("context") |
49 | | - new_context = ( |
50 | | - error_info.context |
51 | | - if len(serialize_value(error_info.context)) <= field_size_limit |
52 | | - else SerializableErrorInfo( |
53 | | - message="(Context truncated due to size limitations)", |
54 | | - stack=[], |
55 | | - cls_name=None, |
56 | | - ) |
57 | | - ) |
58 | | - else: |
59 | | - new_context = truncate_serialized_error( |
60 | | - error_info.context, |
61 | | - field_size_limit, |
62 | | - max_depth=max_depth - 1, |
63 | | - truncations=truncations, |
64 | | - ) |
65 | | - error_info = error_info._replace(context=new_context) |
66 | | - |
67 | | - stack_size_so_far = 0 |
68 | | - truncated_stack = [] |
69 | | - for stack_elem in error_info.stack: |
70 | | - stack_size_so_far += len(stack_elem) |
71 | | - if stack_size_so_far > field_size_limit: |
72 | | - truncations.append("stack") |
73 | | - truncated_stack.append("(TRUNCATED)") |
74 | | - break |
75 | | - |
76 | | - truncated_stack.append(stack_elem) |
77 | | - |
78 | | - error_info = error_info._replace(stack=truncated_stack) |
79 | | - |
80 | | - msg_len = len(error_info.message) |
81 | | - if msg_len > field_size_limit: |
82 | | - truncations.append(f"message from {msg_len} to {field_size_limit}") |
83 | | - error_info = error_info._replace( |
84 | | - message=error_info.message[:field_size_limit] + " (TRUNCATED)" |
85 | | - ) |
86 | | - |
87 | | - if error_info.cls_name and len(error_info.cls_name) > ERROR_CLASS_NAME_SIZE_LIMIT: |
88 | | - truncations.append("cls_name") |
89 | | - error_info = error_info._replace( |
90 | | - cls_name=error_info.cls_name[:ERROR_CLASS_NAME_SIZE_LIMIT] + " (TRUNCATED)" |
91 | | - ) |
92 | | - |
93 | | - return error_info |
94 | | - |
95 | | - |
96 | 5 | DAGSTER_FRAMEWORK_SUBSTRINGS = [ |
97 | 6 | "/site-packages/dagster/", |
98 | 7 | "/python_modules/dagster/dagster", |
|
0 commit comments