@@ -175,21 +175,26 @@ extension SSHClientX on SSHClient {
175175 /// Runs a command and decodes output safely with encoding fallback
176176 ///
177177 /// [systemType] - The system type (affects encoding choice)
178- /// [context] - Optional context for debugging
178+ /// Runs a command and safely decodes the result
179179 Future <String > runSafe (
180180 String command, {
181181 SystemType ? systemType,
182182 String ? context,
183183 }) async {
184+ // Let SSH errors propagate with their original type (e.g., SSHError subclasses)
185+ final result = await run (command);
186+
187+ // Only catch decoding failures and add context
184188 try {
185- final result = await run (command);
186189 return SSHDecoder .decode (
187190 result,
188191 isWindows: systemType == SystemType .windows,
189192 context: context,
190193 );
191- } catch (e) {
192- throw Exception ('Failed to run command${context != null ? " [$context ]" : "" }: $e ' );
194+ } on FormatException catch (e) {
195+ throw Exception (
196+ 'Failed to decode command output${context != null ? " [$context ]" : "" }: $e ' ,
197+ );
193198 }
194199 }
195200
@@ -231,17 +236,32 @@ extension SSHClientX on SSHClient {
231236 final stdoutBytes = stdoutBuilder.takeBytes ();
232237 final stderrBytes = stderrBuilder.takeBytes ();
233238
234- final stdout = SSHDecoder .decode (
235- stdoutBytes,
236- isWindows: systemType == SystemType .windows,
237- context: context != null ? '$context (stdout)' : 'stdout' ,
238- );
239+ // Only catch decoding failures, let other errors propagate
240+ String stdout;
241+ try {
242+ stdout = SSHDecoder .decode (
243+ stdoutBytes,
244+ isWindows: systemType == SystemType .windows,
245+ context: context != null ? '$context (stdout)' : 'stdout' ,
246+ );
247+ } on FormatException catch (e) {
248+ throw Exception (
249+ 'Failed to decode stdout${context != null ? " [$context ]" : "" }: $e ' ,
250+ );
251+ }
239252
240- final stderr = SSHDecoder .decode (
241- stderrBytes,
242- isWindows: systemType == SystemType .windows,
243- context: context != null ? '$context (stderr)' : 'stderr' ,
244- );
253+ String stderr;
254+ try {
255+ stderr = SSHDecoder .decode (
256+ stderrBytes,
257+ isWindows: systemType == SystemType .windows,
258+ context: context != null ? '$context (stderr)' : 'stderr' ,
259+ );
260+ } on FormatException catch (e) {
261+ throw Exception (
262+ 'Failed to decode stderr${context != null ? " [$context ]" : "" }: $e ' ,
263+ );
264+ }
245265
246266 return (stdout, stderr);
247267 }
0 commit comments