Metastatic.Analysis.BusinessLogic.SwallowingException (Metastatic v0.10.4)

View Source

Detects exception handling that swallows exceptions without logging or re-raising.

This analyzer identifies try/catch/rescue blocks that catch exceptions but don't log them or re-raise them, which hides errors and makes debugging difficult.

Cross-Language Applicability

This is a universal anti-pattern in all languages with exception handling:

  • Python: try/except that silently catches
  • JavaScript: try/catch without logging
  • Elixir: try/rescue without logging or re-raising
  • Java/C#: try/catch with empty catch block
  • Ruby: begin/rescue without handling
  • Go: Ignoring error returns (similar concept)

Examples

Bad (Python)

try:
    risky_operation()
except Exception:
    pass  # Silent failure!

Good (Python)

try:
    risky_operation()
except Exception as e:
    logger.error(f"Operation failed: {e}")
    raise

Bad (Elixir)

try do
  risky_operation()
rescue
  _ -> :error  # Exception hidden!
end

Good (Elixir)

try do
  risky_operation()
rescue
  e ->
    Logger.error("Operation failed", error: inspect(e))
    :error
end

Bad (JavaScript)

try {
  riskyOperation();
} catch (e) {
  return null;  // Exception swallowed
}

Good (JavaScript)

try {
  riskyOperation();
} catch (e) {
  console.error('Operation failed:', e);
  throw e;
}

Detection Strategy

Checks exception_handling nodes for:

  1. Catch/rescue handlers that don't contain logging calls
  2. Catch/rescue handlers that don't re-raise the exception
  3. Empty catch blocks