57 status_proto: rpc_pb2.StatusProto,
58) -> Optional[Exception]:
59 """Returns the Python exception that best match the input absl::Status.
61 There are some Status that we expect the MathOpt code to return, for those the
62 matching exceptions are:
63 - InvalidArgument: ValueError
64 - FailedPrecondition: AssertionError
65 - Unimplemented: NotImplementedError
66 - Internal: InternalMathOptError
68 Other Status's are not used by MathOpt, if they are seen a
69 InternalMathOptError is raised (as if the Status was Internal) and the error
70 message contains the unexpected code.
73 status_proto: The input proto to convert to an exception.
76 The corresponding exception. None if the input status is OK.
79 code = _StatusCode(status_proto.code)
81 return InternalMathOptError(
82 f
"unknown C++ error (code = {status_proto.code}):"
83 f
" {status_proto.message}"
86 if code == _StatusCode.OK:
90 error_type: Optional[Type[Exception]] =
None
91 if code == _StatusCode.INVALID_ARGUMENT:
92 error_type = ValueError
93 if code == _StatusCode.FAILED_PRECONDITION:
94 error_type = AssertionError
95 if code == _StatusCode.UNIMPLEMENTED:
96 error_type = NotImplementedError
97 if code == _StatusCode.INTERNAL:
98 error_type = InternalMathOptError
100 if error_type
is not None:
101 return error_type(f
"{status_proto.message} (was C++ {code.name})")
103 return InternalMathOptError(
104 f
"unexpected C++ error {code.name}: {status_proto.message}"