Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::RecordNotUnique should be handled similarly to PG::UniqueViolation #833

Closed
patbenatar opened this issue Mar 3, 2022 · 7 comments · Fixed by #834
Closed
Assignees
Labels

Comments

@patbenatar
Copy link

Rails wraps PG::UniqueViolation in a ActiveRecord::RecordNotUnique. Should this code https://github.com/appsignal/appsignal-ruby/blob/main/lib/appsignal/transaction.rb#L538 handle both?

@patbenatar patbenatar added the bug label Mar 3, 2022
@tombruijn
Copy link
Member

Hi @patbenatar, we can definitely take a look. Do you have an example error message for ActiveRecord::RecordNotUnique. A very small example app would be even better :)

@patbenatar
Copy link
Author

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "example_constraint"
DETAIL: Key (email)=([email protected]) already exists.

We've monkey-patched like so in our app:

module Appsignal
  class Transaction
    def cleaned_error_message(error)
      case error.class.to_s
      when "PG::UniqueViolation", "ActiveRecord::RecordNotUnique"
        error.message.to_s.gsub(/\)=\(.*\)/, ")=(?)")
      else
        error.message.to_s
      end
    end
  end
end

Perhaps rather than adding a list of error classes here, we could make this method extensible via configuration? We use https://github.com/ankane/logstop elsewhere in our application and it'd be great to be able to configure Appsignal to use that too, something like:

config.clean_error_message = ->(msg) { ... }

@tombruijn
Copy link
Member

Looks like you sent a PG::UniqueViolation example, the error message is the same for ActiveRecord::RecordNotUnique? Or is one wrapped in the other?

we could make this method extensible via configuration?

We considered that in the past, but decided not to add it to this first version. We want to take a good look at how best to fix this for all integrations, not just Ruby.

@patbenatar
Copy link
Author

It's wrapped. That's the error message for the AR exception.

Sounds good re: configuration. Simply adding the AR exception class to the list as I've done in my monkey patch would be great for now.

Thanks!

@tombruijn tombruijn self-assigned this Mar 9, 2022
tombruijn added a commit that referenced this issue Mar 9, 2022
Like we've done before for `PG::UniqueViolation`, sanitize the
`ActiveRecord::RecordNotUnique` message.

The `RecordNotUnique` error class is a type of error that wraps around
errors from database adapter gems like the "pg" gem's
`PG::UniqueViolation`. The message for this `RecordNotUnique` error is
the same for `PG::UniqueViolation` in this case, prefixed with error
class name.

Fixes  #833
@tombruijn
Copy link
Member

I've submitted the fix in PR #834.

@tombruijn
Copy link
Member

@patbenatar The fix for this is released in Ruby gem 3.0.25.

@patbenatar
Copy link
Author

@tombruijn thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants