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

NoMethodError: undefined method `byteslice' for []:Array #907

Closed
ericproulx opened this issue Jun 26, 2019 · 3 comments
Closed

NoMethodError: undefined method `byteslice' for []:Array #907

ericproulx opened this issue Jun 26, 2019 · 3 comments
Assignees
Milestone

Comments

@ericproulx
Copy link

Hello,

Sidekiq integrations calls Raven.capture_exception like this:

 Raven.capture_exception(
        ex,
        :message => ex.message,
        :extra => { :sidekiq => context }
      )

Unfortunately, if ex.message isn't a String (in our case, it was a Hash), it fails with NoMethodError: undefined method `byteslice' for []:Array

I know it's weird that ex.message isn't a String, but it works when calling it like this :

# ex is an exception
Raven.capture_exception(ex)

When looking at code, event.message is interpolated properly only if Raven.capture_exception wasn't call with options message

# lib/raven/event.rb

def self.from_exception(exc, options = {}, &block)
      ...
      new(options) do |evt|
        evt.message = "#{exc.class}: #{exc.message}" # interpolated properly

        evt.add_exception_interface(exc)

        yield evt if block
      end
    end

Is it a bug ? I don't know, but I had to to_s my value before raising an exception.

Thanks

@st0012 st0012 added this to the 3.1.0 milestone Aug 14, 2020
@st0012 st0012 added question and removed Type: Bug labels Aug 28, 2020
@st0012
Copy link
Collaborator

st0012 commented Aug 28, 2020

I think it doesn't make much sense to accept a non-string message. However, as a error-reporting SDK it shouldn't cause any new error when taking non-string input. I will open a PR for this.

@st0012
Copy link
Collaborator

st0012 commented Sep 3, 2020

close with #1005

@st0012 st0012 closed this as completed Sep 3, 2020
@akshay-birajdar
Copy link

akshay-birajdar commented Oct 9, 2023

This scenario has a possible regression when the def message in the error class is overridden to return a non-string value. Ideally, we shouldn't do it and yet it was done 😓 .

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "sentry-ruby", "~> 5.11"
  gem "rspec"
end

require "sentry-ruby"
require "rspec/autorun"

RSpec.describe "SentryClient" do
  DUMMY_DSN = 'http://12345:[email protected]/sentry/42'

  let(:configuration) do
    Sentry::Configuration.new.tap do |config|
      config.dsn = DUMMY_DSN
      config.transport.transport_class = Sentry::DummyTransport
    end
  end

  subject { Sentry::Client.new(configuration) }

  it "converts non-string message" do
    NonStringMessageError = Class.new(StandardError) { def message = { foo: "bar" } }
    event = subject.event_from_exception(NonStringMessageError.new)
    expect(event).to be_a(Sentry::ErrorEvent)
    expect(Sentry::Event.get_message_from_exception(event.to_hash)).to match("NonStringMessageError: {:foo=>\"bar\"}")

    # Actual
    # NoMethodError:
    #   undefined method `byteslice' for {:foo=>"bar"}:Hash
    #
    #       @value = exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
    #                                   ^^^^^^^^^^
  end
end

Fix seems to add the following:

--- a/sentry-ruby/lib/sentry/interfaces/single_exception.rb
+++ b/sentry-ruby/lib/sentry/interfaces/single_exception.rb
@@ -22,6 +22,7 @@ module Sentry
         else
           exception.message || ""
         end
+      exception_message = exception_message.inspect unless exception_message.is_a?(String)

       @value = Utils::EncodingHelper.encode_to_utf_8(exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES))

If the sentry team considers this a valid bug report, I would like to submit a fix patch.

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

No branches or pull requests

4 participants