Skip to content

Replace generic Exception catching with specific exception types in locale detection #5

@skempken

Description

@skempken

Problem

The detect_list_names() function uses overly broad exception handling:

# things_jxa.py:102-105
except Exception as e:
    # Fallback to German locale if detection fails
    console.print(f"[yellow]Warning: Could not auto-detect locale, using German. Error: {e}[/yellow]")
    return LOCALE_MAPPINGS["de"]

Issues:

  • Catches all exceptions, including programming errors (NameError, AttributeError, etc.)
  • Makes debugging harder - legitimate bugs are silently suppressed
  • Error message is always "using German" even though the error might be unrelated to localization

Current Implementation

Location: things_jxa.py:102-105

The broad except Exception catches:

  • Expected errors: JXA connection failures, JSON parsing errors
  • Unexpected errors: Programming bugs, import errors, type errors

Proposed Solution

Use specific exception types:

try:
    output = run_jxa(script)
    list_names = json.loads(output)
    # ... detection logic ...
    return detected_mapping

except (RuntimeError, json.JSONDecodeError) as e:
    # Only catch expected JXA/JSON errors
    console.print(f"[yellow]Warning: Could not auto-detect locale, using fallback. Error: {e}[/yellow]")
    return LOCALE_MAPPINGS["de"]  # Or "en" per issue #4
except Exception as e:
    # Let unexpected errors propagate for debugging
    console.print(f"[red]Unexpected error in locale detection: {e}[/red]")
    raise

Or even better, let run_jxa() handle its own errors and only catch JSON issues:

try:
    output = run_jxa(script)  # Already raises RuntimeError for JXA failures
    list_names = json.loads(output)
    # ... detection logic ...
    return detected_mapping

except RuntimeError as e:
    # JXA connection/execution failure
    console.print(f"[yellow]Could not query Things 3 for locale detection. Using fallback.[/yellow]")
    return LOCALE_MAPPINGS["de"]
except json.JSONDecodeError as e:
    # Malformed JSON response
    console.print(f"[yellow]Invalid JXA response format. Using fallback locale.[/yellow]")
    return LOCALE_MAPPINGS["de"]
# Let other exceptions propagate (they indicate bugs)

Impact

Priority: Medium - error handling improvement

Affected code: things_jxa.py:102-105

Breaking changes: None

Benefits:

  • Easier debugging (real bugs aren't hidden)
  • More specific error messages
  • Better error handling practices

Acceptance Criteria

  • Only catch expected exception types (RuntimeError, JSONDecodeError)
  • Programming errors propagate normally for debugging
  • Clear, specific error messages for each failure type
  • Fallback still works when Things 3 is unavailable
  • Unit tests for different error scenarios

Additional Context

This issue was identified during post-implementation review of #1 (multi-locale support).

Related: Python best practices recommend catching specific exceptions rather than bare Exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error-handlingImprovements to error handling and validationrefactoringOptimization of the present structure while maintaining functionality

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions