Skip to content

Commit 6a16c08

Browse files
authored
Rethrow for Better Stack Trace (#1532)
* Drop a trailing space * Rethrow exception from generated method * Generate async methods so throw actually helps
1 parent 4744780 commit 6a16c08

File tree

3 files changed

+468
-105
lines changed

3 files changed

+468
-105
lines changed

InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs

+22-6
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,15 @@ partial class {ns}{classDeclaration}
383383
/// <param name="isTopLevel">True if directly from the type we're generating for, false for methods found on base interfaces</param>
384384
void ProcessRefitMethod(StringBuilder source, IMethodSymbol methodSymbol, bool isTopLevel)
385385
{
386-
WriteMethodOpening(source, methodSymbol, !isTopLevel);
386+
var returnType = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
387+
var (isAsync, @return, configureAwait) = methodSymbol.ReturnType.MetadataName switch
388+
{
389+
"Task" => (true, "await (", ").ConfigureAwait(false)"),
390+
"Task`1" or "ValueTask`1" => (true, "return await (", ").ConfigureAwait(false)"),
391+
_ => (false, "return ", ""),
392+
};
393+
394+
WriteMethodOpening(source, methodSymbol, !isTopLevel, isAsync);
387395

388396
// Build the list of args for the array
389397
var argList = new List<string>();
@@ -411,7 +419,14 @@ void ProcessRefitMethod(StringBuilder source, IMethodSymbol methodSymbol, bool i
411419
source.Append(@$"
412420
var ______arguments = new object[] {{ {string.Join(", ", argList)} }};
413421
var ______func = requestBuilder.BuildRestResultFuncForMethod(""{methodSymbol.Name}"", new global::System.Type[] {{ {string.Join(", ", typeList)} }}{genericString} );
414-
return ({methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})______func(this.Client, ______arguments);
422+
try
423+
{{
424+
{@return}({returnType})______func(this.Client, ______arguments){configureAwait};
425+
}}
426+
catch (global::System.Exception ex)
427+
{{
428+
throw ex;
429+
}}
415430
");
416431

417432
WriteMethodClosing(source);
@@ -500,14 +515,15 @@ void ProcessNonRefitMethod<TContext>(TContext context, Action<TContext, Diagnost
500515
}
501516
}
502517

503-
void WriteMethodOpening(StringBuilder source, IMethodSymbol methodSymbol, bool isExplicitInterface)
518+
void WriteMethodOpening(StringBuilder source, IMethodSymbol methodSymbol, bool isExplicitInterface, bool isAsync = false)
504519
{
505520
var visibility = !isExplicitInterface ? "public " : string.Empty;
521+
var async = isAsync ? "async " : "";
506522

507523
source.Append(@$"
508524

509525
/// <inheritdoc />
510-
{visibility}{methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} ");
526+
{visibility}{async}{methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} ");
511527

512528
if(isExplicitInterface)
513529
{
@@ -527,8 +543,8 @@ void WriteMethodOpening(StringBuilder source, IMethodSymbol methodSymbol, bool i
527543

528544
source.Append(string.Join(", ", list));
529545
}
530-
531-
source.Append(@$") {GenerateConstraints(methodSymbol.TypeParameters, isExplicitInterface)}
546+
547+
source.Append(@$"){GenerateConstraints(methodSymbol.TypeParameters, isExplicitInterface)}
532548
{{");
533549
}
534550

0 commit comments

Comments
 (0)