Skip to content

Commit 508f6ba

Browse files
committed
Include less stack frames in exceptions come from server filters
1 parent 6a43c8b commit 508f6ba

File tree

1 file changed

+45
-50
lines changed

1 file changed

+45
-50
lines changed

Diff for: src/Hangfire.Core/Server/BackgroundJobPerformer.cs

+45-50
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,10 @@ private static PerformedContext InvokeServerFilter(
142142
{
143143
var filter = enumerator.Current!;
144144

145-
try
146-
{
147-
preContext.Profiler.InvokeMeasured(
148-
new KeyValuePair<IServerFilter, PerformingContext>(filter, preContext),
149-
InvokeOnPerforming,
150-
static ctx => $"OnPerforming for {ctx.Value.BackgroundJob.Id}" );
151-
}
152-
catch (Exception filterException) when (filterException.IsCatchableExceptionType())
153-
{
154-
CoreBackgroundJobPerformer.HandleJobPerformanceException(
155-
filterException,
156-
preContext.CancellationToken, preContext.BackgroundJob);
157-
throw;
158-
}
145+
preContext.Profiler.InvokeMeasured(
146+
new KeyValuePair<IServerFilter, PerformingContext>(filter, preContext),
147+
InvokeOnPerforming,
148+
static ctx => $"OnPerforming for {ctx.Value.BackgroundJob.Id}" );
159149

160150
if (preContext.Canceled)
161151
{
@@ -179,21 +169,10 @@ private static PerformedContext InvokeServerFilter(
179169
postContext = new PerformedContext(
180170
preContext, null, false, ex);
181171

182-
try
183-
{
184-
postContext.Profiler.InvokeMeasured(
185-
new KeyValuePair<IServerFilter, PerformedContext>(filter, postContext),
186-
InvokeOnPerformed,
187-
static ctx => $"OnPerformed for {ctx.Value.BackgroundJob.Id}");
188-
}
189-
catch (Exception filterException) when (filterException.IsCatchableExceptionType())
190-
{
191-
CoreBackgroundJobPerformer.HandleJobPerformanceException(
192-
filterException,
193-
postContext.CancellationToken, postContext.BackgroundJob);
194-
195-
throw;
196-
}
172+
postContext.Profiler.InvokeMeasured(
173+
new KeyValuePair<IServerFilter, PerformedContext>(filter, postContext),
174+
InvokeOnPerformed,
175+
static ctx => $"OnPerformed for {ctx.Value.BackgroundJob.Id}");
197176

198177
if (!postContext.ExceptionHandled)
199178
{
@@ -203,34 +182,41 @@ private static PerformedContext InvokeServerFilter(
203182

204183
if (!wasError)
205184
{
206-
try
207-
{
208-
postContext.Profiler.InvokeMeasured(
209-
new KeyValuePair<IServerFilter, PerformedContext>(filter, postContext),
210-
InvokeOnPerformed,
211-
static ctx => $"OnPerformed for {ctx.Value.BackgroundJob.Id}");
212-
}
213-
catch (Exception filterException) when (filterException.IsCatchableExceptionType())
214-
{
215-
CoreBackgroundJobPerformer.HandleJobPerformanceException(
216-
filterException,
217-
postContext.CancellationToken, postContext.BackgroundJob);
218-
219-
throw;
220-
}
185+
postContext.Profiler.InvokeMeasured(
186+
new KeyValuePair<IServerFilter, PerformedContext>(filter, postContext),
187+
InvokeOnPerformed,
188+
static ctx => $"OnPerformed for {ctx.Value.BackgroundJob.Id}");
221189
}
222190

223191
return postContext;
224192
}
225193

226-
private static void InvokeOnPerforming(KeyValuePair<IServerFilter, PerformingContext> x)
194+
private static void InvokeOnPerforming(KeyValuePair<IServerFilter, PerformingContext> ctx)
227195
{
228-
x.Key.OnPerforming(x.Value);
196+
try
197+
{
198+
ctx.Key.OnPerforming(ctx.Value);
199+
}
200+
catch (Exception filterException) when (filterException.IsCatchableExceptionType())
201+
{
202+
CoreBackgroundJobPerformer.HandleJobPerformanceException(
203+
filterException,
204+
ctx.Value.CancellationToken, ctx.Value.BackgroundJob);
205+
}
229206
}
230207

231-
private static void InvokeOnPerformed(KeyValuePair<IServerFilter, PerformedContext> x)
208+
private static void InvokeOnPerformed(KeyValuePair<IServerFilter, PerformedContext> ctx)
232209
{
233-
x.Key.OnPerformed(x.Value);
210+
try
211+
{
212+
ctx.Key.OnPerformed(ctx.Value);
213+
}
214+
catch (Exception filterException) when (filterException.IsCatchableExceptionType())
215+
{
216+
CoreBackgroundJobPerformer.HandleJobPerformanceException(
217+
filterException,
218+
ctx.Value.CancellationToken, ctx.Value.BackgroundJob);
219+
}
234220
}
235221

236222
private static void InvokeServerExceptionFilters(
@@ -246,9 +232,18 @@ private static void InvokeServerExceptionFilters(
246232
}
247233
}
248234

249-
private static void InvokeOnServerException(KeyValuePair<IServerExceptionFilter, ServerExceptionContext> x)
235+
private static void InvokeOnServerException(KeyValuePair<IServerExceptionFilter, ServerExceptionContext> ctx)
250236
{
251-
x.Key.OnServerException(x.Value);
237+
try
238+
{
239+
ctx.Key.OnServerException(ctx.Value);
240+
}
241+
catch (Exception filterException) when (filterException.IsCatchableExceptionType())
242+
{
243+
CoreBackgroundJobPerformer.HandleJobPerformanceException(
244+
filterException,
245+
ctx.Value.CancellationToken, ctx.Value.BackgroundJob);
246+
}
252247
}
253248
}
254249
}

0 commit comments

Comments
 (0)