Skip to content

Commit da73cf1

Browse files
refactor: improve error handling and logging for input files
1 parent ca2eee9 commit da73cf1

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

pre-compute/src/compute/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum ReplicateStatusCause {
2828
#[error("IS_DATASET_REQUIRED environment variable is missing")]
2929
PreComputeIsDatasetRequiredMissing,
3030
#[error("Input file download failed for input {0}")]
31-
PreComputeInputFileDownloadFailed(usize),
31+
PreComputeInputFileDownloadFailed(String),
3232
#[error("Input files number related environment variable is missing")]
3333
PreComputeInputFilesNumberMissing,
3434
#[error("Invalid dataset checksum for dataset {0}")]

pre-compute/src/compute/pre_compute_app.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl PreComputeAppTrait for PreComputeApp {
114114
/// # Returns
115115
///
116116
/// - `Ok(())` if all files are downloaded successfully.
117-
/// - `Err(ReplicateStatusCause::PreComputeInputFileDownloadFailed)` if any file fails to download.
117+
/// - `Err(ReplicateStatusCause::PreComputeInputFileDownloadFailed(url))` if any file fails to download.
118118
///
119119
/// # Panics
120120
///
@@ -126,13 +126,13 @@ impl PreComputeAppTrait for PreComputeApp {
126126
let args = &self.pre_compute_args;
127127
let chain_task_id: &str = &self.chain_task_id;
128128

129-
for (index, url) in args.input_files.iter().enumerate() {
129+
for url in args.input_files.iter() {
130130
info!("Downloading input file [chainTaskId:{chain_task_id}, url:{url}]");
131131

132132
let filename = sha256(url.to_string());
133133
if download_file(url, &args.output_dir, &filename).is_none() {
134134
exit_causes.push(ReplicateStatusCause::PreComputeInputFileDownloadFailed(
135-
index,
135+
url.to_string(),
136136
));
137137
}
138138
}
@@ -317,7 +317,9 @@ mod tests {
317317
let result = app.download_input_files();
318318
assert_eq!(
319319
result.unwrap_err(),
320-
vec![ReplicateStatusCause::PreComputeInputFileDownloadFailed(0)]
320+
vec![ReplicateStatusCause::PreComputeInputFileDownloadFailed(
321+
"https://invalid-url-that-should-fail.com/file.txt".to_string()
322+
)]
321323
);
322324
}
323325

@@ -339,7 +341,9 @@ mod tests {
339341
let result = app.download_input_files();
340342
assert_eq!(
341343
result.unwrap_err(),
342-
vec![ReplicateStatusCause::PreComputeInputFileDownloadFailed(1)]
344+
vec![ReplicateStatusCause::PreComputeInputFileDownloadFailed(
345+
"https://invalid-url-that-should-fail.com/file.txt".to_string()
346+
)]
343347
);
344348

345349
// First file should be downloaded with SHA256 filename

pre-compute/src/compute/pre_compute_args.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl PreComputeArgs {
6969
) {
7070
Ok(output_dir) => output_dir,
7171
Err(e) => {
72+
error!("Failed to read output directory: {e:?}");
7273
return (PreComputeArgs::default(), vec![e]);
7374
}
7475
};
@@ -80,11 +81,13 @@ impl PreComputeArgs {
8081
Ok(s) => match s.to_lowercase().parse::<bool>() {
8182
Ok(value) => value,
8283
Err(_) => {
84+
error!("Invalid boolean format for IS_DATASET_REQUIRED: {s}");
8385
exit_causes.push(ReplicateStatusCause::PreComputeIsDatasetRequiredMissing);
8486
false
8587
}
8688
},
8789
Err(e) => {
90+
error!("Failed to read IS_DATASET_REQUIRED: {e:?}");
8891
exit_causes.push(e);
8992
false
9093
}
@@ -97,11 +100,13 @@ impl PreComputeArgs {
97100
Ok(s) => match s.parse::<usize>() {
98101
Ok(value) => value,
99102
Err(_) => {
103+
error!("Invalid numeric format for IEXEC_BULK_SLICE_SIZE: {s}");
100104
exit_causes.push(ReplicateStatusCause::PreComputeFailedUnknownIssue);
101105
0
102106
}
103107
},
104108
Err(e) => {
109+
error!("Failed to read IEXEC_BULK_SLICE_SIZE: {e:?}");
105110
exit_causes.push(e);
106111
0
107112
}
@@ -118,6 +123,7 @@ impl PreComputeArgs {
118123
) {
119124
Ok(filename) => filename,
120125
Err(e) => {
126+
error!("Failed to read dataset {i} filename: {e:?}");
121127
exit_causes.push(e);
122128
continue;
123129
}
@@ -129,6 +135,7 @@ impl PreComputeArgs {
129135
) {
130136
Ok(url) => url,
131137
Err(e) => {
138+
error!("Failed to read dataset {i} URL: {e:?}");
132139
exit_causes.push(e);
133140
continue;
134141
}
@@ -140,6 +147,7 @@ impl PreComputeArgs {
140147
) {
141148
Ok(checksum) => checksum,
142149
Err(e) => {
150+
error!("Failed to read dataset {i} checksum: {e:?}");
143151
exit_causes.push(e);
144152
continue;
145153
}
@@ -151,6 +159,7 @@ impl PreComputeArgs {
151159
) {
152160
Ok(key) => key,
153161
Err(e) => {
162+
error!("Failed to read dataset {i} key: {e:?}");
154163
exit_causes.push(e);
155164
continue;
156165
}
@@ -166,30 +175,31 @@ impl PreComputeArgs {
166175
Ok(s) => match s.parse::<usize>() {
167176
Ok(value) => value,
168177
Err(_) => {
178+
error!("Invalid numeric format for IEXEC_INPUT_FILES_NUMBER: {s}");
169179
exit_causes.push(ReplicateStatusCause::PreComputeInputFilesNumberMissing);
170180
0
171181
}
172182
},
173183
Err(e) => {
184+
error!("Failed to read IEXEC_INPUT_FILES_NUMBER: {e:?}");
174185
exit_causes.push(e);
175186
0
176187
}
177188
};
178189

179-
let input_files: Vec<String> = (1..=input_files_nb)
180-
.filter_map(|i| {
181-
match get_env_var_or_error(
182-
TeeSessionEnvironmentVariable::IexecInputFileUrlPrefix(i),
183-
ReplicateStatusCause::PreComputeAtLeastOneInputFileUrlMissing(i),
184-
) {
185-
Ok(url) => Some(url),
186-
Err(e) => {
187-
exit_causes.push(e);
188-
None
189-
}
190+
let mut input_files: Vec<String> = Vec::new();
191+
for i in 1..=input_files_nb {
192+
match get_env_var_or_error(
193+
TeeSessionEnvironmentVariable::IexecInputFileUrlPrefix(i),
194+
ReplicateStatusCause::PreComputeAtLeastOneInputFileUrlMissing(i),
195+
) {
196+
Ok(url) => input_files.push(url),
197+
Err(e) => {
198+
error!("Failed to read input file {i} URL: {e:?}");
199+
exit_causes.push(e)
190200
}
191-
})
192-
.collect();
201+
}
202+
}
193203

194204
if !exit_causes.is_empty() {
195205
error!(

0 commit comments

Comments
 (0)