Skip to content

Commit bd60dd4

Browse files
authored
don't use errnos when we didn't see that errno (#926)
1 parent e83d75f commit bd60dd4

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

nexus/src/external_api/console_api.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ pub async fn asset(
285285
Some(static_dir) => find_file(path, &static_dir.join("assets")),
286286
_ => Err(not_found("static_dir undefined")),
287287
}?;
288-
let file_contents =
289-
tokio::fs::read(&file).await.map_err(|_| not_found("EBADF"))?;
288+
let file_contents = tokio::fs::read(&file)
289+
.await
290+
.map_err(|e| not_found(&format!("accessing {:?}: {:#}", file, e)))?;
290291

291292
// Derive the MIME type from the file name
292293
let content_type = mime_guess::from_path(&file)
@@ -316,8 +317,9 @@ async fn serve_console_index(
316317
.to_owned()
317318
.ok_or_else(|| not_found("static_dir undefined"))?;
318319
let file = static_dir.join(PathBuf::from("index.html"));
319-
let file_contents =
320-
tokio::fs::read(&file).await.map_err(|_| not_found("EBADF"))?;
320+
let file_contents = tokio::fs::read(&file)
321+
.await
322+
.map_err(|e| not_found(&format!("accessing {:?}: {:#}", file, e)))?;
321323
Ok(Response::builder()
322324
.status(StatusCode::OK)
323325
.header(http::header::CONTENT_TYPE, "text/html; charset=UTF-8")
@@ -358,27 +360,29 @@ fn find_file(
358360
// If we hit a non-directory thing already and we still have segments
359361
// left in the path, bail. We have nowhere to go.
360362
if !current.is_dir() {
361-
return Err(not_found("ENOENT"));
363+
return Err(not_found("expected a directory"));
362364
}
363365

364366
current.push(segment);
365367

366368
// Don't follow symlinks.
367369
// Error means either the user doesn't have permission to pull
368370
// metadata or the path doesn't exist.
369-
let m = current.symlink_metadata().map_err(|_| not_found("ENOENT"))?;
371+
let m = current
372+
.symlink_metadata()
373+
.map_err(|_| not_found("failed to get file metadata"))?;
370374
if m.file_type().is_symlink() {
371-
return Err(not_found("EMLINK"));
375+
return Err(not_found("attempted to follow a symlink"));
372376
}
373377
}
374378

375379
// can't serve a directory
376380
if current.is_dir() {
377-
return Err(not_found("EISDIR"));
381+
return Err(not_found("expected a non-directory"));
378382
}
379383

380384
if !file_ext_allowed(&current) {
381-
return Err(not_found("EACCES"));
385+
return Err(not_found("file extension not allowed"));
382386
}
383387

384388
Ok(current)
@@ -409,7 +413,7 @@ mod test {
409413
let error = find_file(get_path("tests/static/nonexistent.svg"), &root)
410414
.unwrap_err();
411415
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
412-
assert_eq!(error.internal_message, "ENOENT".to_string());
416+
assert_eq!(error.internal_message, "failed to get file metadata",);
413417
}
414418

415419
#[test]
@@ -419,7 +423,7 @@ mod test {
419423
find_file(get_path("tests/static/a/b/c/nonexistent.svg"), &root)
420424
.unwrap_err();
421425
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
422-
assert_eq!(error.internal_message, "ENOENT".to_string());
426+
assert_eq!(error.internal_message, "failed to get file metadata")
423427
}
424428

425429
#[test]
@@ -429,7 +433,7 @@ mod test {
429433
find_file(get_path("tests/static/assets/a_directory"), &root)
430434
.unwrap_err();
431435
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
432-
assert_eq!(error.internal_message, "EISDIR".to_string());
436+
assert_eq!(error.internal_message, "expected a non-directory");
433437
}
434438

435439
#[test]
@@ -448,7 +452,7 @@ mod test {
448452
// so we 404
449453
let error = find_file(get_path(path_str), &root).unwrap_err();
450454
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
451-
assert_eq!(error.internal_message, "EMLINK".to_string());
455+
assert_eq!(error.internal_message, "attempted to follow a symlink");
452456
}
453457

454458
#[test]
@@ -462,7 +466,7 @@ mod test {
462466
// but it 404s because the path goes through a symlink
463467
let error = find_file(get_path(path_str), &root).unwrap_err();
464468
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
465-
assert_eq!(error.internal_message, "EMLINK".to_string());
469+
assert_eq!(error.internal_message, "attempted to follow a symlink");
466470
}
467471

468472
#[test]
@@ -472,11 +476,11 @@ mod test {
472476
find_file(get_path("tests/static/assets/blocked.ext"), &root)
473477
.unwrap_err();
474478
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
475-
assert_eq!(error.internal_message, "EACCES".to_string());
479+
assert_eq!(error.internal_message, "file extension not allowed",);
476480

477481
let error = find_file(get_path("tests/static/assets/no_ext"), &root)
478482
.unwrap_err();
479483
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
480-
assert_eq!(error.internal_message, "EACCES".to_string());
484+
assert_eq!(error.internal_message, "file extension not allowed");
481485
}
482486
}

0 commit comments

Comments
 (0)