[go: up one dir, main page]

Skip to content

PoC: Add RuboCop cop to catch rescuing StandardError without logging

What does this MR do?

This MR adds a new RuboCop cop Gitlab/RescueStandardErrorWithoutTracking that detects instances where StandardError is rescued but no error is logged or tracked, addressing issue #556969.

Problem

Rescuing StandardError without logging it creates "silent failures" that make debugging nearly impossible since you lose all visibility into what went wrong and when. This has caused issues in the past where:

  1. Errors were not bubbled up during incidents where build trace chunks were improperly archived
  2. Services rescued all StandardErrors and returned error messages directly with a 400 BadRequest response without proper tracking

Solution

The new cop checks for:

  • Rescue blocks that catch StandardError (explicitly or implicitly with bare rescue)
  • Missing calls to Gitlab::ErrorTracking.track_and_raise_for_dev
  • Missing logging statements (logger.error, Rails.logger.error, etc.)
  • Missing re-raise statements

Examples

Bad (will trigger the cop):

begin
  do_something
rescue StandardError => e
  return nil
end

begin
  do_something
rescue => e  # implicit StandardError
  return nil
end

Good (will not trigger the cop):

# Using error tracking
begin
  do_something
rescue StandardError => e
  Gitlab::ErrorTracking.track_and_raise_for_dev(e)
  return nil
end

# Using logging
begin
  do_something
rescue StandardError => e
  logger.error("Something went wrong: #{e.message}")
  return nil
end

# Re-raising the error
begin
  do_something
rescue StandardError => e
  cleanup_resources
  raise
end

# Rescuing specific error types (preferred)
begin
  do_something
rescue ActiveRecord::RecordNotFound => e
  return nil
end

Testing

The cop includes comprehensive RSpec tests covering:

  • Basic StandardError rescue detection
  • Implicit StandardError rescue (bare rescue)
  • Proper error tracking with Gitlab::ErrorTracking.track_and_raise_for_dev
  • Various logging patterns
  • Re-raising scenarios
  • Specific error type handling
  • Mixed error type scenarios
  • Error handling in conditionals and blocks

Related Issues

Closes #556969

Checklist

  • Added RuboCop cop implementation
  • Added comprehensive RSpec tests
  • Followed existing GitLab RuboCop cop patterns
  • Included proper documentation and examples

Merge request reports

Loading