Skip to content

Commit fb9a7b9

Browse files
dubstackGoogle-ML-Automation
authored andcommitted
Add utility methods to wrap absl::Status's and messages with and XLA error code.
PiperOrigin-RevId: 855483429
1 parent 9258aaa commit fb9a7b9

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

xla/error/error_codes.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,40 @@ inline std::string GetErrorUrl(ErrorCode code) {
137137
return absl::StrCat("https://openxla.org/xla/errors/error_", id);
138138
}
139139

140+
// Returns the error message with the standard XLA Error Code formatting:
141+
// "EXXXX: ErrorName:\n<Original Message>\nSee <URL> for more details."
142+
inline std::string FormatMessageWithCode(absl::string_view message,
143+
ErrorCode code) {
144+
return absl::StrCat(GetErrorCodeAndName(code), ":\n", message, "\nSee ",
145+
GetErrorUrl(code), " for more details.");
146+
}
147+
148+
// Wraps an existing status with the standard XLA Error Code formatting:
149+
// "EXXXX: ErrorName:\n<Original Message>\nSee <URL> for more details."
150+
inline absl::Status AnnotateWithCode(const absl::Status& original,
151+
ErrorCode code) {
152+
if (original.ok()) {
153+
return original;
154+
}
155+
156+
absl::Status new_status(original.code(),
157+
FormatMessageWithCode(original.message(), code));
158+
159+
// Copy over the payloads and source locations from the original status.
160+
original.ForEachPayload(
161+
[&new_status](absl::string_view type_url, const absl::Cord& payload) {
162+
new_status.SetPayload(type_url, payload);
163+
});
164+
165+
#if defined(PLATFORM_GOOGLE)
166+
for (const auto& loc : original.GetSourceLocations()) {
167+
new_status.AddSourceLocation(loc);
168+
}
169+
#endif // PLATFORM_GOOGLE
170+
171+
return new_status;
172+
}
173+
140174
// The following three macros implement a factory pattern for creating
141175
// absl::Status objects. This pattern is necessary to reliably capture the
142176
// caller's source location while supporting variadic arguments.

0 commit comments

Comments
 (0)