Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit a348336

Browse files
committed
Added SentryEvent
- Added SentryEvent class. - Added Capture() and CaptureAsync() methods on IRavenClient taking SentryEvent. - Obsoleted the CaptureMessage() and CaptureException() methods and their async counterparts on SentryEvent. - Refactored all code to using the new SentryEvent class as its main code path. - Added a couple of tests.
1 parent acb587d commit a348336

9 files changed

Lines changed: 364 additions & 112 deletions

File tree

src/app/SharpRaven/Data/IJsonPacketFactory.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,14 @@
3434
namespace SharpRaven.Data
3535
{
3636
/// <summary>
37-
/// Factory interface for creating
38-
/// <see cref="JsonPacket" />s. To simply adjust the values of a
39-
/// packet before it is sent to Sentry, inherit
40-
/// <see cref="JsonPacketFactory" /> and override
41-
/// its
42-
/// <see cref="JsonPacketFactory.OnCreate" /> method.
37+
/// Factory interface for creating <see cref="JsonPacket" />s.
38+
/// To simply adjust the values of a packet before it is sent to Sentry, inherit
39+
/// <see cref="JsonPacketFactory" /> and override its <see cref="JsonPacketFactory.OnCreate" /> method.
4340
/// </summary>
4441
public interface IJsonPacketFactory
4542
{
4643
/// <summary>
47-
/// Creates a new instance of
48-
/// <see cref="JsonPacket" /> for the specified
44+
/// Creates a new instance of <see cref="JsonPacket" /> for the specified
4945
/// <paramref name="project" />.
5046
/// </summary>
5147
/// <param name="project">The project.</param>
@@ -66,11 +62,8 @@ JsonPacket Create(string project,
6662

6763

6864
/// <summary>
69-
/// Creates a new instance of
70-
/// <see cref="JsonPacket" /> for the specified
71-
/// <paramref name="project" />, with the
72-
/// given
73-
/// <paramref name="exception" />.
65+
/// Creates a new instance of <see cref="JsonPacket" /> for the specified
66+
/// <paramref name="project" />, with the given <paramref name="exception" />.
7467
/// </summary>
7568
/// <param name="project">The project.</param>
7669
/// <param name="exception">The <see cref="Exception" /> to capture.</param>
@@ -80,11 +73,8 @@ JsonPacket Create(string project,
8073
/// <param name="fingerprint">The custom fingerprint to annotate the captured <paramref name="message" /> with.</param>
8174
/// <param name="extra">The extra metadata to send with the captured <paramref name="exception" />.</param>
8275
/// <returns>
83-
/// A new instance of
84-
/// <see cref="JsonPacket" /> for the specified
85-
/// <paramref name="project" />, with the
86-
/// given
87-
/// <paramref name="exception" />.
76+
/// A new instance of <see cref="JsonPacket" /> for the specified
77+
/// <paramref name="project" />, with the given <paramref name="exception" />.
8878
/// </returns>
8979
JsonPacket Create(string project,
9080
Exception exception,
@@ -93,5 +83,18 @@ JsonPacket Create(string project,
9383
IDictionary<string, string> tags = null,
9484
string[] fingerprint = null,
9585
object extra = null);
86+
87+
88+
/// <summary>
89+
/// Creates a new instance of <see cref="JsonPacket" /> for the specified
90+
/// <paramref name="project" />, with the given <paramref name="event" />.
91+
/// </summary>
92+
/// <param name="project">The project.</param>
93+
/// <param name="event">The event to capture.</param>
94+
/// <returns>
95+
/// A new instance of <see cref="JsonPacket" /> for the specified
96+
/// <paramref name="project" />, with the given <paramref name="event" />.
97+
/// </returns>
98+
JsonPacket Create(string project, SentryEvent @event);
9699
}
97100
}

src/app/SharpRaven/Data/JsonPacket.cs

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
// Copyright (c) 2014 The Sentry Team and individual contributors.
44
// All rights reserved.
5-
//
5+
//
66
// Redistribution and use in source and binary forms, with or without modification, are permitted
77
// provided that the following conditions are met:
8-
//
8+
//
99
// 1. Redistributions of source code must retain the above copyright notice, this list of
1010
// conditions and the following disclaimer.
11-
//
11+
//
1212
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of
1313
// conditions and the following disclaimer in the documentation and/or other materials
1414
// provided with the distribution.
15-
//
15+
//
1616
// 3. Neither the name of the Sentry nor the names of its contributors may be used to
1717
// endorse or promote products derived from this software without specific prior written
1818
// permission.
19-
//
19+
//
2020
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
2121
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2222
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
@@ -55,46 +55,24 @@ public JsonPacket(string project, Exception exception)
5555
if (exception == null)
5656
throw new ArgumentNullException("exception");
5757

58-
Message = exception.Message;
59-
60-
if (exception.TargetSite != null)
61-
{
62-
// ReSharper disable ConditionIsAlwaysTrueOrFalse => not for dynamic types.
63-
Culprit = String.Format("{0} in {1}",
64-
((exception.TargetSite.ReflectedType == null)
65-
? "<dynamic type>"
66-
: exception.TargetSite.ReflectedType.FullName),
67-
exception.TargetSite.Name);
68-
// ReSharper restore ConditionIsAlwaysTrueOrFalse
69-
}
70-
71-
Exceptions = new List<SentryException>();
58+
Initialize(exception);
59+
}
7260

73-
for (Exception currentException = exception;
74-
currentException != null;
75-
currentException = currentException.InnerException)
76-
{
77-
SentryException sentryException = new SentryException(currentException)
78-
{
79-
Module = currentException.Source,
80-
Type = currentException.GetType().Name,
81-
Value = currentException.Message
82-
};
8361

84-
Exceptions.Add(sentryException);
85-
}
62+
/// <summary>Initializes a new instance of the <see cref="JsonPacket"/> class.</summary>
63+
/// <param name="project">The project.</param>
64+
/// <param name="event">The event.</param>
65+
public JsonPacket(string project, SentryEvent @event)
66+
: this(project)
67+
{
68+
if (@event == null)
69+
throw new ArgumentNullException("event");
8670

87-
// ReflectionTypeLoadException doesn't contain much useful info in itself, and needs special handling
88-
ReflectionTypeLoadException reflectionTypeLoadException = exception as ReflectionTypeLoadException;
89-
if (reflectionTypeLoadException != null)
90-
{
91-
foreach (Exception loaderException in reflectionTypeLoadException.LoaderExceptions)
92-
{
93-
SentryException sentryException = new SentryException(loaderException);
71+
if (@event.Exception != null)
72+
Initialize(@event.Exception);
9473

95-
Exceptions.Add(sentryException);
96-
}
97-
}
74+
Message = @event.Message != null ? @event.Message.ToString() : null;
75+
MessageObject = @event.Message;
9876
}
9977

10078

@@ -156,6 +134,12 @@ private JsonPacket()
156134
[JsonProperty(PropertyName = "culprit", NullValueHandling = NullValueHandling.Ignore)]
157135
public string Culprit { get; set; }
158136

137+
/// <summary>
138+
/// Identifies the operating environment (e.g. production).
139+
/// </summary>
140+
[JsonProperty(PropertyName = "environment", NullValueHandling = NullValueHandling.Ignore)]
141+
public string Environment { get; set; }
142+
159143
/// <summary>
160144
/// Hexadecimal string representing a uuid4 value.
161145
/// </summary>
@@ -177,6 +161,12 @@ private JsonPacket()
177161
[JsonProperty(PropertyName = "extra", NullValueHandling = NullValueHandling.Ignore)]
178162
public object Extra { get; set; }
179163

164+
/// <summary>
165+
/// Gets or sets the fingerprint used for custom grouping
166+
/// </summary>
167+
[JsonProperty(PropertyName = "fingerprint", NullValueHandling = NullValueHandling.Ignore)]
168+
public string[] Fingerprint { get; set; }
169+
180170
/// <summary>
181171
/// The record severity.
182172
/// Defaults to error.
@@ -250,18 +240,6 @@ private JsonPacket()
250240
[JsonProperty(PropertyName = "server_name", NullValueHandling = NullValueHandling.Ignore)]
251241
public string ServerName { get; set; }
252242

253-
/// <summary>
254-
/// Gets or sets the fingerprint used for custom grouping
255-
/// </summary>
256-
[JsonProperty(PropertyName = "fingerprint", NullValueHandling = NullValueHandling.Ignore)]
257-
public string[] Fingerprint { get; set; }
258-
259-
/// <summary>
260-
/// Identifies the operating environment (e.g. production).
261-
/// </summary>
262-
[JsonProperty(PropertyName = "environment", NullValueHandling = NullValueHandling.Ignore)]
263-
public string Environment { get; set; }
264-
265243
/// <summary>
266244
/// A map or list of tags for this event.
267245
/// </summary>
@@ -308,5 +286,50 @@ public override string ToString()
308286
{
309287
return JsonConvert.SerializeObject(this);
310288
}
289+
290+
291+
private void Initialize(Exception exception)
292+
{
293+
Message = exception.Message;
294+
295+
if (exception.TargetSite != null)
296+
{
297+
// ReSharper disable ConditionIsAlwaysTrueOrFalse => not for dynamic types.
298+
Culprit = String.Format("{0} in {1}",
299+
((exception.TargetSite.ReflectedType == null)
300+
? "<dynamic type>"
301+
: exception.TargetSite.ReflectedType.FullName),
302+
exception.TargetSite.Name);
303+
// ReSharper restore ConditionIsAlwaysTrueOrFalse
304+
}
305+
306+
Exceptions = new List<SentryException>();
307+
308+
for (Exception currentException = exception;
309+
currentException != null;
310+
currentException = currentException.InnerException)
311+
{
312+
SentryException sentryException = new SentryException(currentException)
313+
{
314+
Module = currentException.Source,
315+
Type = currentException.GetType().Name,
316+
Value = currentException.Message
317+
};
318+
319+
Exceptions.Add(sentryException);
320+
}
321+
322+
// ReflectionTypeLoadException doesn't contain much useful info in itself, and needs special handling
323+
ReflectionTypeLoadException reflectionTypeLoadException = exception as ReflectionTypeLoadException;
324+
if (reflectionTypeLoadException != null)
325+
{
326+
foreach (Exception loaderException in reflectionTypeLoadException.LoaderExceptions)
327+
{
328+
SentryException sentryException = new SentryException(loaderException);
329+
330+
Exceptions.Add(sentryException);
331+
}
332+
}
333+
}
311334
}
312-
}
335+
}

src/app/SharpRaven/Data/JsonPacketFactory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ public JsonPacket Create(string project,
120120
}
121121

122122

123+
/// <summary>
124+
/// Creates a new instance of <see cref="JsonPacket" /> for the specified
125+
/// <paramref name="project" />, with the given <paramref name="event" />.
126+
/// </summary>
127+
/// <param name="project">The project.</param>
128+
/// <param name="event">The event to capture.</param>
129+
/// <returns>
130+
/// A new instance of <see cref="JsonPacket" /> for the specified
131+
/// <paramref name="project" />, with the given <paramref name="event" />.
132+
/// </returns>
133+
public JsonPacket Create(string project, SentryEvent @event)
134+
{
135+
var json = new JsonPacket(project, @event);
136+
return OnCreate(json);
137+
}
138+
139+
123140
/// <summary>
124141
/// Called when the <see cref="JsonPacket"/> has been created. Can be overridden to
125142
/// adjust the values of the <paramref name="jsonPacket"/> before it is sent to Sentry.

0 commit comments

Comments
 (0)