Skip to content

Commit 753828f

Browse files
authored
Ensure exceptions use standard logging
Route exceptions through the standard logging mechanism and expand sanitizer/test coverage.
1 parent 6623cb8 commit 753828f

57 files changed

Lines changed: 1622 additions & 430 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/OpenClaw.Connection/GatewayConnectionManager.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ tunnel.SshPort is < 1 or > 65535 ||
289289
{
290290
await lifecycle.ConnectAsync(ct);
291291
}
292-
// slopwatch-ignore: SW003 Shutdown cancellation or disposal is expected and the caller already preserves the safe state.
293-
catch (OperationCanceledException) { }
292+
catch (OperationCanceledException) { /* Expected: connect was cancelled. */ }
294293
catch (Exception ex)
295294
{
296295
_logger.Error($"[ConnMgr] Connect failed: {ex.Message}");
@@ -655,8 +654,7 @@ private bool TryScheduleOperatorTokenRecovery(string message, long gen)
655654
if (Interlocked.Read(ref _generation) != gen || _disposed) return;
656655
await ReconnectAsync();
657656
}
658-
// slopwatch-ignore: SW003 Shutdown cancellation or disposal is expected and the caller already preserves the safe state.
659-
catch (ObjectDisposedException) { }
657+
catch (ObjectDisposedException) { /* Expected: connection manager disposed during reconnect. */ }
660658
catch (Exception ex)
661659
{
662660
_logger.Warn($"[ConnMgr] Operator token recovery reconnect failed: {ex.Message}");
@@ -1241,8 +1239,8 @@ private async Task DisposeCoreAsync()
12411239
{
12421240
if (semaphoreEntered)
12431241
{
1244-
// slopwatch-ignore: SW003 Cleanup is best-effort; failure cannot improve caller state and the original outcome is preserved.
1245-
try { _transitionSemaphore.Release(); } catch { }
1242+
try { _transitionSemaphore.Release(); }
1243+
catch (Exception ex) { _logger.Debug($"[ConnMgr] Dispose: transition semaphore release failed: {ex.Message}"); }
12461244
_transitionSemaphore.Dispose();
12471245
}
12481246

‎src/OpenClaw.Connection/SshTunnelService.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public void Stop()
106106
}
107107
finally
108108
{
109-
// slopwatch-ignore: SW003 Cleanup is best-effort; failure cannot improve caller state and the original outcome is preserved.
110-
try { _process.Dispose(); } catch { }
109+
try { _process.Dispose(); }
110+
catch (Exception disposeEx) { _logger.Debug($"SshTunnelService.Stop: process dispose failed: {disposeEx.Message}"); }
111111
_process = null;
112112
_lastSpec = null;
113113
CurrentBrowserProxyLocalPort = 0;
@@ -173,8 +173,8 @@ private void StartProcess(string user, string host, int remotePort, int localPor
173173
LastError = $"SSH tunnel exited unexpectedly with code {exitCode}.";
174174
StartedAtUtc = null;
175175
Status = TunnelStatus.Failed;
176-
// slopwatch-ignore: SW003 Cleanup is best-effort; failure cannot improve caller state and the original outcome is preserved.
177-
try { process.Dispose(); } catch { }
176+
try { process.Dispose(); }
177+
catch (Exception disposeEx) { _logger.Debug($"SshTunnelService: process dispose after unexpected exit failed: {disposeEx.Message}"); }
178178
_process = null;
179179
_lastSpec = null;
180180
CurrentBrowserProxyLocalPort = 0;

‎src/OpenClaw.SetupEngine.UI/LogFileLauncher.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ public static void RevealInExplorer(string? logPath)
6565
});
6666
}
6767
}
68-
// slopwatch-ignore: SW003 Audited non-critical fallback is intentional and the caller preserves safe behavior without this work.
69-
catch
68+
catch (Exception ex)
7069
{
71-
// best effort — the link is informational
70+
// best effort — the link is informational; if shell open fails
71+
// the user can navigate to the log path manually.
72+
System.Diagnostics.Trace.WriteLine($"LogFileLauncher.OpenContainingFolder: {ex.GetType().Name}: {ex.Message}");
7273
}
7374
}
7475

‎src/OpenClaw.SetupEngine/AtomicFile.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ private static void TryDeleteTemp(string tempPath)
5353
}
5454
catch (Exception ex)
5555
{
56-
System.Diagnostics.Debug.WriteLine($"Failed to delete temporary file '{tempPath}': {ex.Message}");
56+
// Best-effort temp cleanup; no logger available in this static helper.
57+
System.Diagnostics.Trace.WriteLine($"AtomicFile.TryDeleteTemp('{tempPath}'): {ex.GetType().Name}: {ex.Message}");
5758
}
5859
}
5960
}

‎src/OpenClaw.SetupEngine/CommandRunner.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ public Task<CommandResult> RunInWslAsync(
176176

177177
private static void TryKill(Process process)
178178
{
179-
// slopwatch-ignore: SW003 Cleanup is best-effort; failure cannot improve caller state and the original outcome is preserved.
180-
try { process.Kill(entireProcessTree: true); } catch { /* best effort */ }
179+
try { process.Kill(entireProcessTree: true); }
180+
catch (Exception ex)
181+
{
182+
// Best effort — process may already be exiting; no logger in this static helper.
183+
Trace.WriteLine($"CommandRunner.TryKill: {ex.GetType().Name}: {ex.Message}");
184+
}
181185
}
182186

183187
private sealed class BoundedOutputBuffer(int maxChars)

‎src/OpenClaw.SetupEngine/Program.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static async Task<int> Main(string[] args)
9999
// cannot truncate the active run's log or journal files.
100100
using var logger = new SetupLogger(config.LogPath, Enum.TryParse<LogLevel>(config.LogLevel, true, out var lvl) ? lvl : LogLevel.Trace);
101101
var journalPath = Path.ChangeExtension(config.LogPath, ".journal.jsonl");
102-
using var journal = new TransactionJournal(journalPath);
102+
using var journal = new TransactionJournal(journalPath, logger);
103103
var commands = new CommandRunner(logger);
104104
using var cts = new CancellationTokenSource();
105105

‎src/OpenClaw.SetupEngine/RetryExecutor.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public static async Task<StepResult> ExecuteWithRetry(
3737
}
3838
catch (Exception ex)
3939
{
40-
logger.Error($"Step '{stepId}' threw exception (attempt {attempt}/{policy.MaxAttempts}): {ex.Message}");
40+
// Logged at Warn here because we may still retry; if we exhaust retries,
41+
// SetupLogger.StepCompleted will emit the authoritative Error entry with
42+
// the full exception once the failing StepResult is returned.
43+
logger.Warn($"Step '{stepId}' threw exception (attempt {attempt}/{policy.MaxAttempts}): {ex.Message}");
4144
lastResult = StepResult.Fail($"Unhandled exception: {ex.Message}", ex);
4245
}
4346

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace OpenClaw.SetupEngine;
2+
3+
internal static class SetupDiagnostics
4+
{
5+
public static void TryWriteStderrWarning(string message)
6+
{
7+
try { Console.Error.WriteLine($"WARN: {message}"); }
8+
catch { }
9+
}
10+
}

‎src/OpenClaw.SetupEngine/SetupLogger.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ public void StepStarted(string stepId, string displayName)
4646
=> Write(LogLevel.Info, $"step.started: {displayName}", new { step_id = stepId });
4747

4848
public void StepCompleted(string stepId, StepResult result, TimeSpan elapsed)
49-
=> Write(LogLevel.Info, $"step.completed: {stepId} → {result.Outcome}", new { step_id = stepId, outcome = result.Outcome.ToString(), message = result.Message, elapsed_ms = elapsed.TotalMilliseconds });
49+
{
50+
Write(LogLevel.Info, $"step.completed: {stepId} → {result.Outcome}", new { step_id = stepId, outcome = result.Outcome.ToString(), message = result.Message, elapsed_ms = elapsed.TotalMilliseconds });
51+
if (result.Error is not null)
52+
{
53+
// StepResult.Fail(message, ex) preserves the original exception so
54+
// callers don't have to log it inline at every catch site. Surface
55+
// it here at Error level so the cause is never silently dropped.
56+
Write(LogLevel.Error, $"step.exception: {stepId}: {result.Error.GetType().Name}: {result.Error.Message}", new { step_id = stepId, exception_type = result.Error.GetType().FullName, exception = result.Error.ToString() });
57+
}
58+
}
5059

5160
public void CommandStarted(string exe, string[] args, TimeSpan timeout)
5261
=> Write(LogLevel.Debug, $"cmd.start: {exe} {Sanitize(string.Join(' ', args))}", new { exe, args = args.Select(Sanitize).ToArray(), timeout_ms = timeout.TotalMilliseconds });

‎src/OpenClaw.SetupEngine/SetupPipeline.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,16 @@ public async Task<PipelineResult> RunAsync(SetupContext ctx)
157157
continue;
158158
}
159159

160-
// Step failed — handle rollback if configured
161-
ctx.Logger.Error($"Step '{step.Id}' failed: {result.Message}");
160+
// Step failed — handle rollback if configured.
161+
// When result.Error is set, StepCompleted already emitted an Error log for the
162+
// exception, so we downgrade the summary to Warn to avoid duplicate Errors.
163+
// When there is no exception (StepResult.Fail(message)), StepCompleted does NOT
164+
// emit Error, so we keep this summary at Error so failed steps remain visible on
165+
// Error-filtered dashboards.
166+
if (result.Error is null)
167+
ctx.Logger.Error($"SetupPipeline: Step '{step.Id}' failed: {result.Message}");
168+
else
169+
ctx.Logger.Warn($"SetupPipeline: Step '{step.Id}' failed: {result.Message}");
162170

163171
if (ctx.Config.RollbackOnFailure)
164172
{

0 commit comments

Comments
 (0)