Skip to content

Commit 74e20ff

Browse files
committed
refactor(oxfmt): Not panic on expectable error (#16149)
Fixes #16104
1 parent 0d23774 commit 74e20ff

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

apps/oxfmt/src/service.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@ impl FormatService {
7676
let source_type = enable_jsx_source_type(entry.source_type);
7777

7878
let allocator = self.allocator_pool.get();
79-
let source_text = read_to_string(path).expect("Failed to read file");
79+
let Ok(source_text) = read_to_string(path) else {
80+
// This happens if `.ts` for MPEG-TS binary is attempted to be formatted
81+
let diagnostics = DiagnosticService::wrap_diagnostics(
82+
self.cwd.clone(),
83+
path,
84+
"",
85+
vec![
86+
OxcDiagnostic::error(format!("Failed to read file: {}", path.display()))
87+
.with_help("This may be due to the file being a binary or inaccessible."),
88+
],
89+
);
90+
tx_error.send(diagnostics).unwrap();
91+
return;
92+
};
8093

8194
let ret = Parser::new(&allocator, &source_text, source_type)
8295
.with_options(get_parse_options())
@@ -113,13 +126,14 @@ impl FormatService {
113126
let code = match formatted.print() {
114127
Ok(printed) => printed.into_code(),
115128
Err(err) => {
116-
let oxc_diagnostic =
117-
OxcDiagnostic::error(format!("Failed to print formatted code: {err}"));
118129
let diagnostics = DiagnosticService::wrap_diagnostics(
119130
self.cwd.clone(),
120131
path,
121132
&source_text,
122-
vec![oxc_diagnostic],
133+
vec![OxcDiagnostic::error(format!(
134+
"Failed to print formatted code: {}\n{err}",
135+
path.display()
136+
))],
123137
);
124138
tx_error.send(diagnostics).unwrap();
125139
return;

apps/oxfmt/src/walk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Walk {
2626
let mut inner = ignore::WalkBuilder::new(
2727
target_paths
2828
.first()
29-
.expect("Expected paths parameter to Walk::build() to contain at least one path."),
29+
.expect("`target_paths` never be empty, should have at least `cwd`"),
3030
);
3131
if let Some(paths) = target_paths.get(1..) {
3232
for path in paths {

0 commit comments

Comments
 (0)