@@ -13,10 +13,13 @@ defmodule Sentry.Envelope do
13
13
defstruct [ :event_id , :items ]
14
14
15
15
@ 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.
17
20
"""
18
21
@ 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
20
23
% __MODULE__ {
21
24
event_id: event_id ,
22
25
items: events
@@ -25,36 +28,33 @@ defmodule Sentry.Envelope do
25
28
26
29
@ doc """
27
30
Encodes the envelope into its binary representation.
31
+
32
+ For now, we support only envelopes with a single event in them.
28
33
"""
29
34
@ 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
31
36
json_library = Config . json_library ( )
32
37
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
58
58
end
59
59
60
60
@ doc """
@@ -79,23 +79,6 @@ defmodule Sentry.Envelope do
79
79
end
80
80
end
81
81
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
-
99
82
#
100
83
# Decoding
101
84
#
0 commit comments