Skip to content

Commit

Permalink
Rethrow for Better Stack Trace (#1532)
Browse files Browse the repository at this point in the history
* Drop a trailing space

* Rethrow exception from generated method

* Generate async methods so throw actually helps
  • Loading branch information
dahlbyk authored Jul 7, 2023
1 parent 4744780 commit 6a16c08
Show file tree
Hide file tree
Showing 3 changed files with 468 additions and 105 deletions.
28 changes: 22 additions & 6 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,15 @@ partial class {ns}{classDeclaration}
/// <param name="isTopLevel">True if directly from the type we're generating for, false for methods found on base interfaces</param>
void ProcessRefitMethod(StringBuilder source, IMethodSymbol methodSymbol, bool isTopLevel)
{
WriteMethodOpening(source, methodSymbol, !isTopLevel);
var returnType = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var (isAsync, @return, configureAwait) = methodSymbol.ReturnType.MetadataName switch
{
"Task" => (true, "await (", ").ConfigureAwait(false)"),
"Task`1" or "ValueTask`1" => (true, "return await (", ").ConfigureAwait(false)"),
_ => (false, "return ", ""),
};

WriteMethodOpening(source, methodSymbol, !isTopLevel, isAsync);

// Build the list of args for the array
var argList = new List<string>();
Expand Down Expand Up @@ -411,7 +419,14 @@ void ProcessRefitMethod(StringBuilder source, IMethodSymbol methodSymbol, bool i
source.Append(@$"
var ______arguments = new object[] {{ {string.Join(", ", argList)} }};
var ______func = requestBuilder.BuildRestResultFuncForMethod(""{methodSymbol.Name}"", new global::System.Type[] {{ {string.Join(", ", typeList)} }}{genericString} );
return ({methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})______func(this.Client, ______arguments);
try
{{
{@return}({returnType})______func(this.Client, ______arguments){configureAwait};
}}
catch (global::System.Exception ex)
{{
throw ex;
}}
");

WriteMethodClosing(source);
Expand Down Expand Up @@ -500,14 +515,15 @@ void ProcessNonRefitMethod<TContext>(TContext context, Action<TContext, Diagnost
}
}

void WriteMethodOpening(StringBuilder source, IMethodSymbol methodSymbol, bool isExplicitInterface)
void WriteMethodOpening(StringBuilder source, IMethodSymbol methodSymbol, bool isExplicitInterface, bool isAsync = false)
{
var visibility = !isExplicitInterface ? "public " : string.Empty;
var async = isAsync ? "async " : "";

source.Append(@$"

/// <inheritdoc />
{visibility}{methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} ");
{visibility}{async}{methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} ");

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

source.Append(string.Join(", ", list));
}
source.Append(@$") {GenerateConstraints(methodSymbol.TypeParameters, isExplicitInterface)}

source.Append(@$"){GenerateConstraints(methodSymbol.TypeParameters, isExplicitInterface)}
{{");
}

Expand Down
Loading

0 comments on commit 6a16c08

Please sign in to comment.