AutoGraph-specific error metadata. See base class.
| 69 | |
| 70 | |
| 71 | class _ErrorMetadata(errors.ErrorMetadataBase): |
| 72 | """AutoGraph-specific error metadata. See base class.""" |
| 73 | |
| 74 | def create_exception(self, source_error): |
| 75 | preferred_type = type(source_error) |
| 76 | if issubclass(preferred_type, errors_impl.OpError): |
| 77 | # Best-effort unpacking of OpError exceptions. |
| 78 | # TODO(mdan): Use a mechanism that is more future-proof. |
| 79 | init_argspec = tf_inspect.getfullargspec(preferred_type.__init__) |
| 80 | message = self.get_message() |
| 81 | init_args = tuple(init_argspec.args) |
| 82 | # At the time of this writing, TF errors either take 3 or 4 arguments, |
| 83 | # with the fourth being error_code. |
| 84 | if init_args == ('self', 'node_def', 'op', 'message', 'error_code'): |
| 85 | return preferred_type( |
| 86 | node_def=source_error.node_def, |
| 87 | op=source_error.op, |
| 88 | message=message, |
| 89 | error_code=self.error_code) |
| 90 | elif init_args == ('self', 'node_def', 'op', 'message'): |
| 91 | if 'error_code' in init_argspec.kwonlyargs: |
| 92 | return preferred_type( |
| 93 | node_def=source_error.node_def, |
| 94 | op=source_error.op, |
| 95 | message=message, |
| 96 | errro_code=self.error_code) |
| 97 | else: |
| 98 | return preferred_type( |
| 99 | node_def=source_error.node_def, |
| 100 | op=source_error.op, |
| 101 | message=message) |
| 102 | |
| 103 | elif preferred_type in (AutoGraphError, ConversionError, StagingError, |
| 104 | errors_impl.InaccessibleTensorError, |
| 105 | errors_impl.OperatorNotAllowedInGraphError): |
| 106 | return preferred_type(self.get_message()) |
| 107 | |
| 108 | exc = super(_ErrorMetadata, self).create_exception(source_error) |
| 109 | if exc is not None: |
| 110 | return exc |
| 111 | |
| 112 | # Note: While changing an error's message property to change the message it |
| 113 | # displays will probably work a lot of times, there is no standard way in |
| 114 | # Python to do that. The safest way is therefore to create a new exception. |
| 115 | # For user defined exceptions, we could define an interface that allowed |
| 116 | # them to work under this mechanism. |
| 117 | return StagingError(self.get_message()) |
| 118 | |
| 119 | |
| 120 | class StackTraceMapper(tf_stack.StackTraceMapper): |