@@ -13,10 +13,13 @@ defmodule Sentry.Envelope do
1313 defstruct [ :event_id , :items ]
1414
1515 @ doc """
16- Creates a new envelope containing the given events.
16+ Creates a new envelope containing the given event.
17+
18+ Envelopes can only have a single element of type "event", so that's why we
19+ restrict on a single-element list.
1720 """
1821 @ spec new ( [ Event . t ( ) , ... ] ) :: t ( )
19- def new ( [ % Event { event_id: event_id } | _rest ] = events ) do
22+ def new ( [ % Event { event_id: event_id } ] = events ) do
2023 % __MODULE__ {
2124 event_id: event_id ,
2225 items: events
@@ -25,36 +28,33 @@ defmodule Sentry.Envelope do
2528
2629 @ doc """
2730 Encodes the envelope into its binary representation.
31+
32+ For now, we support only envelopes with a single event in them.
2833 """
2934 @ spec to_binary ( t ( ) ) :: { :ok , binary ( ) } | { :error , any ( ) }
30- def to_binary ( % __MODULE__ { } = envelope ) do
35+ def to_binary ( % __MODULE__ { items: [ % Event { } = event ] } = envelope ) do
3136 json_library = Config . json_library ( )
3237
33- encoded_items =
34- Enum . map ( envelope . items , fn item ->
35- case encode_item ( item , json_library ) do
36- { :ok , encoded_item } ->
37- type =
38- if is_struct ( item , Event ) do
39- "event"
40- else
41- raise "unexpected item in envelope: #{ inspect ( item ) } "
42- end
43-
44- [
45- ~s( {"type": "#{ type } ", "length": #{ byte_size ( encoded_item ) } }\n ) ,
46- encoded_item ,
47- ?\n
48- ]
49-
50- { :error , _reason } = error ->
51- throw ( error )
52- end
53- end )
54-
55- { :ok , IO . iodata_to_binary ( [ encode_headers ( envelope ) | encoded_items ] ) }
56- catch
57- { :error , reason } -> { :error , reason }
38+ headers_iodata =
39+ case envelope . event_id do
40+ nil -> "{{}}\n "
41+ event_id -> ~s( {"event_id":"#{ event_id } "}\n )
42+ end
43+
44+ case event |> Sentry.Client . render_event ( ) |> json_library . encode ( ) do
45+ { :ok , encoded_event } ->
46+ body = [
47+ headers_iodata ,
48+ ~s( {"type": "event", "length": #{ byte_size ( encoded_event ) } }\n ) ,
49+ encoded_event ,
50+ ?\n
51+ ]
52+
53+ { :ok , IO . iodata_to_binary ( body ) }
54+
55+ { :error , _reason } = error ->
56+ error
57+ end
5858 end
5959
6060 @ doc """
@@ -79,23 +79,6 @@ defmodule Sentry.Envelope do
7979 end
8080 end
8181
82- #
83- # Encoding
84- #
85-
86- defp encode_headers ( % __MODULE__ { } = envelope ) do
87- case envelope . event_id do
88- nil -> "{{}}\n "
89- event_id -> ~s( {"event_id":"#{ event_id } "}\n )
90- end
91- end
92-
93- defp encode_item ( % Event { } = event , json_library ) do
94- event
95- |> Sentry.Client . render_event ( )
96- |> json_library . encode ( )
97- end
98-
9982 #
10083 # Decoding
10184 #
0 commit comments