Skip to content

fix: improve exception handling in reconnaissance tools#465

Open
lxcxjxhx wants to merge 1 commit into
aliasrobotics:mainfrom
lxcxjxhx:fix/broad-exception-handling
Open

fix: improve exception handling in reconnaissance tools#465
lxcxjxhx wants to merge 1 commit into
aliasrobotics:mainfrom
lxcxjxhx:fix/broad-exception-handling

Conversation

@lxcxjxhx

@lxcxjxhx lxcxjxhx commented Jul 7, 2026

Copy link
Copy Markdown

Problem

The reconnaissance tools (c99.py and shodan.py) use overly broad except Exception clauses that:

  1. Mask real errors: Catching all exceptions hides programming errors and unexpected failures
  2. Missing timeout protection: API calls have no timeout, causing indefinite hangs
  3. Poor error visibility: Silent failures make debugging difficult
  4. Inconsistent error handling: Different tools handle the same error types differently

Solution

1. Replace broad exceptions with specific types

# Before
except Exception:
    return None

# After
except requests.exceptions.Timeout:
    logger.warning("API request timed out")
    return None
except requests.exceptions.ConnectionError:
    logger.warning("Connection error")
    return None
except requests.exceptions.HTTPError as e:
    logger.warning("HTTP error: %s", e)
    return None
except requests.exceptions.RequestException as e:
    logger.warning("Request failed: %s", e)
    return None

2. Add timeout to all API calls

# Before
response = requests.get(base_url, params=params)

# After
response = requests.get(base_url, params=params, timeout=30)

3. Use raise_for_status() for HTTP errors

# Before
if response.status_code != 200:
    return None

# After
response.raise_for_status()

4. Add structured logging

  • Import logging module
  • Create module-level logger
  • Log warnings for recoverable errors
  • Log debug info for non-critical issues

Impact

  • Better error visibility: Specific exception types reveal actual failure modes
  • Prevent hangs: 30-second timeout on all API calls
  • Easier debugging: Structured logging shows what went wrong
  • Consistent behavior: All reconnaissance tools now follow the same error handling pattern
  • No breaking changes: Return values remain the same, only error handling improved

Testing

  • Verified all exception types are properly caught
  • Confirmed timeout behavior works as expected
  • Checked logging output format
  • Ensured no changes to function signatures or return types

- Replace broad 'except Exception' with specific exception types
- Add 30-second timeout to all API calls to prevent hangs
- Use raise_for_status() for proper HTTP error handling
- Add structured logging for better error visibility
- Improve error messages in c99.py and shodan.py tools
@lxcxjxhx

lxcxjxhx commented Jul 8, 2026

Copy link
Copy Markdown
Author

Testing permissions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant