Skip to content

Commit 40fef2c

Browse files
christollidayfacebook-github-bot
authored andcommitted
Rename context_for_key to string_tag
Summary: Next diff writes these to error_tags and not just category keys so giving them a better name. Using a struct with a String and not just a bare String in order to potentially add TagMetadata for string tags later. Reviewed By: iguridi Differential Revision: D71156098 fbshipit-source-id: eeca8f7c24f8958c74f580e119a74266785187fd
1 parent 2a0754d commit 40fef2c

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

app/buck2_client_ctx/src/subscribers/classify_server_stderr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) fn classify_server_stderr(
4141

4242
let error = if let Some(trace) = extract_trace(stderr) {
4343
if tag != ErrorTag::ServerSigterm {
44-
error.context_for_key(&format!("crash({})", trace.trace_key()))
44+
error.string_tag(&format!("crash({})", trace.trace_key()))
4545
} else if let Some(signal_line) = trace.signal_line {
4646
// Keep this because the PID that (might have) sent it could be useful.
4747
// *** Signal 15 (SIGTERM) (0x2b08100000ab5) received by PID 1762297 (pthread TID 0x7f6650339640) (linux TID 1762297) (maybe from PID 2741, UID 176257) (code: 0), stack trace: ***

app/buck2_error/src/context_value.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ pub enum ContextValue {
1919
Dyn(Arc<str>),
2020
Tags(SmallVec<[crate::ErrorTag; 1]>),
2121
Typed(Arc<dyn TypedContext>),
22-
// Stable value for category key
23-
Key(Arc<str>),
2422
StarlarkError(StarlarkContext),
23+
StringTag(StringTag),
24+
}
25+
26+
#[derive(allocative::Allocative, Debug, Clone, Eq, PartialEq)]
27+
pub struct StringTag {
28+
pub tag: String,
2529
}
2630

2731
impl ContextValue {
@@ -31,7 +35,7 @@ impl ContextValue {
3135
Self::Dyn(..) => true,
3236
Self::Typed(e) => e.should_display(),
3337
Self::Tags(_) => false,
34-
Self::Key(..) => false,
38+
Self::StringTag(..) => false,
3539
Self::StarlarkError(..) => false,
3640
}
3741
}
@@ -48,7 +52,7 @@ impl ContextValue {
4852
(ContextValue::Typed(left), ContextValue::Typed(right)) => {
4953
assert!(left.eq(&**right))
5054
}
51-
(ContextValue::Key(a), ContextValue::Key(b)) => {
55+
(ContextValue::StringTag(a), ContextValue::StringTag(b)) => {
5256
assert_eq!(a, b);
5357
}
5458
(ContextValue::StarlarkError(a), ContextValue::StarlarkError(b)) => {
@@ -65,7 +69,7 @@ impl std::fmt::Display for ContextValue {
6569
Self::Dyn(v) => f.write_str(v),
6670
Self::Tags(tags) => write!(f, "{:?}", tags),
6771
Self::Typed(v) => std::fmt::Display::fmt(v, f),
68-
Self::Key(v) => f.write_str(v),
72+
Self::StringTag(v) => f.write_str(&v.tag),
6973
Self::StarlarkError(v) => write!(f, "{}", v),
7074
}
7175
}

app/buck2_error/src/error.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::classify::tag_is_generic;
1919
use crate::classify::tag_is_hidden;
2020
use crate::context_value::ContextValue;
2121
use crate::context_value::StarlarkContext;
22+
use crate::context_value::StringTag;
2223
use crate::context_value::TypedContext;
2324
use crate::format::into_anyhow_for_format;
2425
use crate::root::ErrorRoot;
@@ -178,15 +179,12 @@ impl Error {
178179

179180
let key_tags = key_tags.into_iter().map(|tag| tag.as_str_name().to_owned());
180181

181-
let context_key_values = self.iter_context().filter_map(|kind| match kind {
182-
ContextValue::Key(val) => Some(val.to_string()),
183-
_ => None,
184-
});
182+
let string_tags = self.string_tags();
185183

186184
let values: Vec<String> = source_location
187185
.into_iter()
188186
.chain(key_tags)
189-
.chain(context_key_values)
187+
.chain(string_tags)
190188
.collect();
191189

192190
values.join(":").to_owned()
@@ -200,9 +198,11 @@ impl Error {
200198
Self(Arc::new(ErrorKind::WithContext(context.into(), self)))
201199
}
202200

203-
pub fn context_for_key(self, context: &str) -> Self {
201+
pub fn string_tag(self, context: &str) -> Self {
204202
Self(Arc::new(ErrorKind::WithContext(
205-
ContextValue::Key(context.into()),
203+
ContextValue::StringTag(StringTag {
204+
tag: context.into(),
205+
}),
206206
self,
207207
)))
208208
}
@@ -259,6 +259,15 @@ impl Error {
259259
})
260260
}
261261

262+
pub fn string_tags(&self) -> Vec<String> {
263+
self.iter_context()
264+
.filter_map(|kind| match kind {
265+
ContextValue::StringTag(val) => Some(val.tag.clone()),
266+
_ => None,
267+
})
268+
.collect()
269+
}
270+
262271
/// Get all the tags that have been added to this error
263272
pub fn tags(&self) -> Vec<crate::ErrorTag> {
264273
let mut tags: Vec<_> = self.tags_unsorted().collect();

app/buck2_error/src/starlark_error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn error_with_starlark_context(
9797
ContextValue::StarlarkError(_) => {
9898
return buck2_error.context_for_starlark_backtrace(starlark_context);
9999
}
100-
ContextValue::Tags(_) | ContextValue::Key(_) => {
100+
ContextValue::Tags(_) | ContextValue::StringTag(_) => {
101101
context_stack.push(context_value.clone())
102102
}
103103
}
@@ -313,7 +313,7 @@ mod tests {
313313
let e = crate::Error::from(FullMetadataError);
314314
let e = e.context(context_error);
315315
let e = e.tag([error_tag]);
316-
let e = e.context_for_key(context_key);
316+
let e = e.string_tag(context_key);
317317

318318
let context_popped = error_with_starlark_context(&e, starlark_context);
319319

app/buck2_execute/src/re/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn re_error(
7878
};
7979
let buck2_error: buck2_error::Error = err.clone().into();
8080

81-
buck2_error.context(err).context_for_key(&group.to_string())
81+
buck2_error.context(err).string_tag(&group.to_string())
8282
}
8383

8484
pub(crate) async fn with_error_handler<T>(

app/buck2_server_commands/src/commands/install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ async fn send_file(
910910
error = error.tag([category_tag]);
911911

912912
for tag in error_detail.tags {
913-
error = error.context_for_key(&tag);
913+
error = error.string_tag(&tag);
914914
}
915915
outcome = Err(error);
916916
}

0 commit comments

Comments
 (0)