@@ -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, " \n See " ,
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