Skip to content

Commit

Permalink
Reworked Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Feb 24, 2022
1 parent b869ecf commit d137362
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
@@ -1,6 +1,5 @@
using System.Runtime.Serialization;
using System.Xml.Linq;
using static System.Enum;
using static StrawberryShake.CodeGeneration.CSharp.Names;
using static StrawberryShake.CodeGeneration.CSharp.RequestOptions;

Expand Down Expand Up @@ -79,6 +78,8 @@ public static GeneratorRequest Parse(string fileName)
string.IsNullOrEmpty(rootDirectory) ? null : rootDirectory,
string.IsNullOrEmpty(defaultNamespace) ? null : defaultNamespace,
string.IsNullOrEmpty(persistedQueryDirectory) ? null : persistedQueryDirectory,
string.IsNullOrEmpty(optionString) ? Default : Parse<RequestOptions>(optionString));
string.IsNullOrEmpty(optionString)
? Default
: (RequestOptions)Enum.Parse(typeof(RequestOptions), optionString));
}
}
Expand Up @@ -89,7 +89,7 @@ private static GeneratorDocument DeserializeGeneratorDocument(XElement documentE
return new GeneratorDocument(
nameAttribute.Value,
sourceTextElement.Value,
Enum.Parse<GeneratorDocumentKind>(kindAttribute.Value),
(GeneratorDocumentKind)Enum.Parse(typeof(GeneratorDocumentKind), kindAttribute.Value),
hashAttribute?.Value,
pathAttribute?.Value);
}
Expand Down
Expand Up @@ -2,7 +2,7 @@

namespace StrawberryShake.CodeGeneration.CSharp.Analyzers;

public class CSharpGeneratorClient
internal sealed class CSharpGeneratorClient
{
private readonly string _codeGenServer;

Expand All @@ -13,29 +13,44 @@ public CSharpGeneratorClient(string codeGenServer)

public GeneratorResponse Execute(GeneratorRequest request)
{
var fileSink = RequestFormatter.Format(request);

var childProcess = Process.Start(
new ProcessStartInfo
try
{
var fileSink = RequestFormatter.Format(request);

var childProcess = Process.Start(
new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"\"{_codeGenServer}\" \"{fileSink}\"",
CreateNoWindow = true,
UseShellExecute = false
});

if (childProcess is null)
{
FileName = "dotnet",
Arguments = $"\"{_codeGenServer}\" \"{fileSink}\"",
CreateNoWindow = true,
UseShellExecute = false
});
return CreateErrorResponse("Unable to generate client!");
}

if (childProcess is null)
{
throw new Exception("Unable to generate client!");
}
childProcess.WaitForExit();

childProcess.WaitForExit();
if (childProcess.ExitCode != 0)
{
return CreateErrorResponse("An error happened while generating the code.");
}

if (childProcess.ExitCode != 0)
return ResponseFormatter.Take(fileSink);
}
catch (Exception ex)
{
throw new Exception("An error happened while generating the code.");
return CreateErrorResponse(ex);
}

return ResponseFormatter.Take(fileSink);
}

private static GeneratorResponse CreateErrorResponse(Exception exception)
=> CreateErrorResponse(exception.Message + Environment.NewLine + exception.StackTrace);

private static GeneratorResponse CreateErrorResponse(string message)
=> new GeneratorResponse(
Array.Empty<GeneratorDocument>(),
new[] { new GeneratorError("SSG0005", "Generator Error", message) });
}
Expand Up @@ -3,7 +3,7 @@

namespace StrawberryShake.CodeGeneration.CSharp.Analyzers;

public static class DiagnosticErrorHelper
internal static class DiagnosticErrorHelper
{
public const string ErrorCategory = "StrawberryShakeGenerator";

Expand Down
Expand Up @@ -3,7 +3,7 @@

namespace StrawberryShake.CodeGeneration.CSharp.Analyzers;

public static class GeneratorExecutionContextExtensions
internal static class GeneratorExecutionContextExtensions
{
public static void ReportDiagnostic(
this GeneratorExecutionContext context,
Expand Down
@@ -1,6 +1,6 @@
namespace StrawberryShake.CodeGeneration.CSharp.Analyzers;

public static class SourceDocumentExtensions
internal static class SourceDocumentExtensions
{
public static IEnumerable<GeneratorDocument> SelectCSharp(
this IEnumerable<GeneratorDocument> documents) =>
Expand Down
Expand Up @@ -5,7 +5,7 @@
namespace StrawberryShake.CodeGeneration.CSharp.Analyzers;

[Generator]
public class StrawberryShakeSourceGenerator : ISourceGenerator
public sealed class StrawberryShakeSourceGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
Expand Down

0 comments on commit d137362

Please sign in to comment.