@@ -41,7 +41,8 @@ public final class InternalSentrySdk {
41
41
@ Nullable
42
42
public static Scope getCurrentScope () {
43
43
final @ NotNull AtomicReference <Scope > scopeRef = new AtomicReference <>();
44
- HubAdapter .getInstance ().withScope (scopeRef ::set );
44
+ //noinspection Convert2MethodRef
45
+ HubAdapter .getInstance ().withScope (scope -> scopeRef .set (scope ));
45
46
return scopeRef .get ();
46
47
}
47
48
@@ -60,15 +61,14 @@ public static Map<String, Object> serializeScope(
60
61
final @ NotNull Context context ,
61
62
final @ NotNull SentryAndroidOptions options ,
62
63
final @ Nullable Scope scope ) {
64
+
63
65
final @ NotNull Map <String , Object > data = new HashMap <>();
64
66
if (scope == null ) {
65
67
return data ;
66
68
}
67
-
68
- final @ NotNull ILogger logger = options .getLogger ();
69
- final @ NotNull ObjectWriter writer = new MapObjectWriter (data );
70
-
71
69
try {
70
+ final @ NotNull ILogger logger = options .getLogger ();
71
+ final @ NotNull ObjectWriter writer = new MapObjectWriter (data );
72
72
73
73
final @ NotNull DeviceInfoUtil deviceInfoUtil = DeviceInfoUtil .getInstance (context , options );
74
74
final @ NotNull Device deviceInfo = deviceInfoUtil .collectDeviceInformation (true , true );
@@ -114,7 +114,7 @@ public static Map<String, Object> serializeScope(
114
114
writer .name ("fingerprint" ).value (logger , scope .getFingerprint ());
115
115
writer .name ("level" ).value (logger , scope .getLevel ());
116
116
writer .name ("breadcrumbs" ).value (logger , scope .getBreadcrumbs ());
117
- } catch (Exception e ) {
117
+ } catch (Throwable e ) {
118
118
options .getLogger ().log (SentryLevel .ERROR , "Could not serialize scope." , e );
119
119
return new HashMap <>();
120
120
}
@@ -124,54 +124,62 @@ public static Map<String, Object> serializeScope(
124
124
125
125
/**
126
126
* Captures the provided envelope. Compared to {@link IHub#captureEvent(SentryEvent)} this method
127
- * - will not enrich events with additional data (e.g. scope) - will not execute beforeSend: it's
128
- * up to the caller to take care of this - will not perform any sampling: it's up to the caller to
129
- * take care of this - will enrich the envelope with a Session updates is applicable
127
+ * <br>
128
+ * - will not enrich events with additional data (e.g. scope)<br>
129
+ * - will not execute beforeSend: it's up to the caller to take care of this<br>
130
+ * - will not perform any sampling: it's up to the caller to take care of this<br>
131
+ * - will enrich the envelope with a Session update if applicable<br>
130
132
*
131
133
* @param envelopeData the serialized envelope data
132
- * @return The Id (SentryId object) of the event
133
- * @throws Exception In case the provided envelope could not be parsed / is invalid
134
+ * @return The Id (SentryId object) of the event, or null in case the envelope could not be
135
+ * captured
134
136
*/
135
- public static SentryId captureEnvelope (final @ NotNull byte [] envelopeData ) throws Exception {
137
+ @ Nullable
138
+ public static SentryId captureEnvelope (final @ NotNull byte [] envelopeData ) {
136
139
final @ NotNull IHub hub = HubAdapter .getInstance ();
137
140
final @ NotNull SentryOptions options = hub .getOptions ();
138
141
139
- final @ NotNull ISerializer serializer = options .getSerializer ();
140
- final @ Nullable SentryEnvelope envelope =
141
- options .getEnvelopeReader ().read (new ByteArrayInputStream (envelopeData ));
142
- if (envelope == null ) {
143
- throw new IllegalArgumentException ("Envelope could not be read" );
144
- }
142
+ try {
143
+ final @ NotNull ISerializer serializer = options .getSerializer ();
144
+ final @ Nullable SentryEnvelope envelope =
145
+ options .getEnvelopeReader ().read (new ByteArrayInputStream (envelopeData ));
146
+ if (envelope == null ) {
147
+ return null ;
148
+ }
145
149
146
- final @ NotNull List <SentryEnvelopeItem > envelopeItems = new ArrayList <>();
150
+ final @ NotNull List <SentryEnvelopeItem > envelopeItems = new ArrayList <>();
147
151
148
- // determine session state based on events inside envelope
149
- @ Nullable Session .State status = null ;
150
- boolean crashedOrErrored = false ;
151
- for (SentryEnvelopeItem item : envelope .getItems ()) {
152
- envelopeItems .add (item );
152
+ // determine session state based on events inside envelope
153
+ @ Nullable Session .State status = null ;
154
+ boolean crashedOrErrored = false ;
155
+ for (SentryEnvelopeItem item : envelope .getItems ()) {
156
+ envelopeItems .add (item );
153
157
154
- final SentryEvent event = item .getEvent (serializer );
155
- if (event != null ) {
156
- if (event .isCrashed ()) {
157
- status = Session .State .Crashed ;
158
- }
159
- if (event .isCrashed () || event .isErrored ()) {
160
- crashedOrErrored = true ;
158
+ final SentryEvent event = item .getEvent (serializer );
159
+ if (event != null ) {
160
+ if (event .isCrashed ()) {
161
+ status = Session .State .Crashed ;
162
+ }
163
+ if (event .isCrashed () || event .isErrored ()) {
164
+ crashedOrErrored = true ;
165
+ }
161
166
}
162
167
}
163
- }
164
168
165
- // update session and add it to envelope if necessary
166
- final @ Nullable Session session = updateSession (hub , options , status , crashedOrErrored );
167
- if (session != null ) {
168
- final SentryEnvelopeItem sessionItem = SentryEnvelopeItem .fromSession (serializer , session );
169
- envelopeItems .add (sessionItem );
170
- }
169
+ // update session and add it to envelope if necessary
170
+ final @ Nullable Session session = updateSession (hub , options , status , crashedOrErrored );
171
+ if (session != null ) {
172
+ final SentryEnvelopeItem sessionItem = SentryEnvelopeItem .fromSession (serializer , session );
173
+ envelopeItems .add (sessionItem );
174
+ }
171
175
172
- final SentryEnvelope repackagedEnvelope =
173
- new SentryEnvelope (envelope .getHeader (), envelopeItems );
174
- return hub .captureEnvelope (repackagedEnvelope );
176
+ final SentryEnvelope repackagedEnvelope =
177
+ new SentryEnvelope (envelope .getHeader (), envelopeItems );
178
+ return hub .captureEnvelope (repackagedEnvelope );
179
+ } catch (Throwable t ) {
180
+ options .getLogger ().log (SentryLevel .ERROR , "Failed to capture envelope" , t );
181
+ }
182
+ return null ;
175
183
}
176
184
177
185
@ Nullable
@@ -181,7 +189,7 @@ private static Session updateSession(
181
189
final @ Nullable Session .State status ,
182
190
final boolean crashedOrErrored ) {
183
191
final @ NotNull AtomicReference <Session > sessionRef = new AtomicReference <>();
184
- hub .withScope (
192
+ hub .configureScope (
185
193
scope -> {
186
194
final @ Nullable Session session = scope .getSession ();
187
195
if (session != null ) {
0 commit comments