Skip to content

Commit 2c8bcc8

Browse files
authored
refactor!: collecting a stream with an error raises an error (nushell#16738)
Sort of a follow up to nushell#16732, makes the stream-collecting error handling universal. This fixes similar bugs everywhere. ## Release notes summary - What our users need to know > TODO(release-notes): Merge this with nushell#16732's notes, this just generalizes the fix for all commands. ## Tasks after submitting N/A
1 parent eecb4dd commit 2c8bcc8

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

crates/nu-cmd-lang/src/core_commands/describe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn run(
278278
let subtype = if options.no_collect {
279279
Value::string("any", head)
280280
} else {
281-
describe_value(stream.into_value(), head, engine_state)
281+
describe_value(stream.into_debug_value(), head, engine_state)
282282
};
283283
Value::record(
284284
record! {
@@ -294,7 +294,7 @@ fn run(
294294
} else if options.no_collect {
295295
Value::string("stream", head)
296296
} else {
297-
let value = stream.into_value();
297+
let value = stream.into_debug_value();
298298
let base_description = value.get_type().to_string();
299299
Value::string(format!("{base_description} (stream)"), head)
300300
}

crates/nu-command/src/filters/each.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,7 @@ fn each_map(value: Value, closure: &mut ClosureEval, head: Span) -> Result<Value
163163
let is_error = value.is_error();
164164
closure
165165
.run_with_value(value)
166-
.and_then(|pipeline_data| match pipeline_data {
167-
// TODO: Should collecting a stream with an error immediately raise the
168-
// error by default (like we do here) be the default?
169-
PipelineData::ListStream(stream, ..) => stream
170-
.into_iter()
171-
.map(Value::unwrap_error)
172-
.collect::<Result<Vec<_>, _>>()
173-
.map(|vals| Value::list(vals, head)),
174-
data => data.into_value(head),
175-
})
166+
.and_then(|pipeline_data| pipeline_data.into_value(head))
176167
.map_err(|error| chain_error_with_input(error, is_error, span))
177168
}
178169

crates/nu-command/src/path/join.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn run(call: &Call, args: &Arguments, input: PipelineData) -> Result<PipelineDat
170170
match input {
171171
PipelineData::Value(val, md) => Ok(PipelineData::value(handle_value(val, args, head), md)),
172172
PipelineData::ListStream(stream, ..) => Ok(PipelineData::value(
173-
handle_value(stream.into_value(), args, head),
173+
handle_value(stream.into_value()?, args, head),
174174
metadata,
175175
)),
176176
PipelineData::ByteStream(stream, ..) => Ok(PipelineData::value(

crates/nu-protocol/src/pipeline/list_stream.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,20 @@ impl ListStream {
7575
}
7676

7777
/// Collect the values of a [`ListStream`] into a list [`Value`].
78-
pub fn into_value(self) -> Value {
78+
///
79+
/// If any of the values in the stream is a [Value::Error], its inner [ShellError] is returned.
80+
pub fn into_value(self) -> Result<Value, ShellError> {
81+
Ok(Value::list(
82+
self.stream
83+
.map(Value::unwrap_error)
84+
.collect::<Result<_, _>>()?,
85+
self.span,
86+
))
87+
}
88+
89+
/// Collect the values of a [`ListStream`] into a [`Value::List`], preserving [Value::Error]
90+
/// items for debugging purposes.
91+
pub fn into_debug_value(self) -> Value {
7992
Value::list(self.stream.collect(), self.span)
8093
}
8194

crates/nu-protocol/src/pipeline/pipeline_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl PipelineData {
192192
Ok(value)
193193
}
194194
}
195-
PipelineData::ListStream(stream, ..) => Ok(stream.into_value()),
195+
PipelineData::ListStream(stream, ..) => stream.into_value(),
196196
PipelineData::ByteStream(stream, ..) => stream.into_value(),
197197
}
198198
}

0 commit comments

Comments
 (0)