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