|
499 | 499 | end |
500 | 500 | end |
501 | 501 |
|
| 502 | + context "when sending raises an encoding error" do |
| 503 | + let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) } |
| 504 | + let(:envelope) { subject.envelope_from_event(event) } |
| 505 | + |
| 506 | + before do |
| 507 | + allow(subject).to receive(:send_data).and_raise(EncodingError, "simulated send error") |
| 508 | + end |
| 509 | + |
| 510 | + it "does not handle the error as a serialization failure" do |
| 511 | + expect { subject.send_envelope(envelope) }.to raise_error(EncodingError, "simulated send error") |
| 512 | + expect(io.string).not_to match(/Failed to serialize envelope/) |
| 513 | + end |
| 514 | + end |
| 515 | + |
502 | 516 | context "transaction event" do |
503 | 517 | let(:transaction) do |
504 | 518 | Sentry::Transaction.new(name: "test transaction", op: "rack.request") |
|
652 | 666 | end |
653 | 667 | end |
654 | 668 |
|
655 | | - context "when JSON.generate raises an encoding error (json 3.0+ behavior)" do |
656 | | - # json 3.0+ raises Encoding::UndefinedConversionError (a subclass of |
657 | | - # EncodingError) instead of just warning when JSON.generate encounters |
658 | | - # a String tagged with a non-UTF-8 encoding that contains bytes invalid |
659 | | - # for the target encoding. Simulate that here regardless of the json |
660 | | - # gem version actually loaded, as a last-resort safety net for cases |
661 | | - # not already covered by sanitizing data at the point it's filled in |
662 | | - # (e.g. breadcrumb data, log attributes). |
663 | | - let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) } |
664 | | - let(:envelope) { subject.envelope_from_event(event) } |
| 669 | + context "when JSON.generate raises an encoding error for an item (json 3.0+ behavior)" do |
| 670 | + let(:bad_payload) { { message: "bad payload" } } |
| 671 | + let(:good_payload) { { message: "good payload" } } |
| 672 | + let(:envelope) do |
| 673 | + Sentry::Envelope.new.tap do |new_envelope| |
| 674 | + new_envelope.add_item({ type: "event" }, bad_payload) |
| 675 | + new_envelope.add_item({ type: "event" }, good_payload) |
| 676 | + end |
| 677 | + end |
665 | 678 |
|
666 | 679 | before do |
667 | | - allow(JSON).to receive(:generate).and_raise(EncodingError, "simulated json 3.0 encoding error") |
| 680 | + allow(JSON).to receive(:generate).and_wrap_original do |original, value| |
| 681 | + raise EncodingError, "simulated json 3.0 encoding error" if value.equal?(bad_payload) |
| 682 | + |
| 683 | + original.call(value) |
| 684 | + end |
668 | 685 | end |
669 | 686 |
|
670 | | - it "does not raise, logs the failure, and records a lost event instead of sending" do |
671 | | - expect(subject).not_to receive(:send_data) |
| 687 | + it "skips the failed item, sends the remaining items, and records the loss" do |
| 688 | + expect(subject).to receive(:send_data) do |data| |
| 689 | + expect(data).to include("good payload") |
| 690 | + expect(data).not_to include("bad payload") |
| 691 | + end |
672 | 692 |
|
673 | 693 | expect { subject.send_envelope(envelope) }.not_to raise_error |
674 | 694 |
|
675 | | - expect(io.string).to match(/Failed to serialize envelope/) |
| 695 | + expect(io.string).to match(/Failed to serialize envelope item/) |
676 | 696 | expect(subject).to have_recorded_lost_event(:send_error, 'error') |
677 | 697 | end |
678 | 698 | end |
|
0 commit comments