Skip to content

Commit

Permalink
feat(compiler): catching unhandled exceptions
Browse files Browse the repository at this point in the history
- Adding exception handling in the compiler console app to catch any
unhandled exceptions.

- Adding an issue template for bug tracking and linking it in the
console output if an unhandled exception occurs.

Part of #60
  • Loading branch information
RossAndersonDev committed Nov 6, 2020
1 parent 5b2c2e6 commit 5c71d68
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 33 deletions.
29 changes: 29 additions & 0 deletions issue_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Bug: <Title>

<!--- Provide a general summary of the issue in the Title above -->

## Expected Behavior

<!--- Tell us what should happen -->

## Current Behavior

<!--- Tell us what happens instead of the expected behavior -->

## Steps to Reproduce

<!--- Provide a unit test and/or a set of steps to reproduce this bug-->

- [ ] Unit test provided

### Steps

1.

2.

3.

## Possible Solution

<!--- Not obligatory, but suggest a fix/reason for the bug, -->
5 changes: 5 additions & 0 deletions src/Thrift.Net.Compiler/ExitCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ public enum ExitCode
/// Compilation has failed because of one or more errors.
/// </summary>
CompilationFailed = 2,

/// <summary>
/// Compilation has failed because of an unhandled exception.
/// </summary>
UnhandledException = 3,
}
}
78 changes: 45 additions & 33 deletions src/Thrift.Net.Compiler/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Thrift.Net.Compiler
namespace Thrift.Net.Compiler
{
using System;
using System.CommandLine;
Expand Down Expand Up @@ -58,50 +58,62 @@ public class Program
/// </returns>
public static int Main(FileInfo input, DirectoryInfo outputDirectory, IConsole console = null)
{
if (!input.Exists)
try
{
console.Error.Write($"The specified input file '{input.Name}' could not be found.{Environment.NewLine}");
return (int)ExitCode.InputFileNotFound;
}
if (!input.Exists)
{
console.Error.Write($"The specified input file '{input.Name}' could not be found.{Environment.NewLine}");
return (int)ExitCode.InputFileNotFound;
}

if (!outputDirectory.Exists)
{
outputDirectory.Create();
}
if (!outputDirectory.Exists)
{
outputDirectory.Create();
}

console.Out.Write($"Starting compilation of {input.Name}{Environment.NewLine}");
console.Out.Write($"Starting compilation of {input.Name}{Environment.NewLine}");

var thriftFile = FileProvider.Create(
new DirectoryInfo(Directory.GetCurrentDirectory()),
input,
outputDirectory);
var thriftFile = FileProvider.Create(
new DirectoryInfo(Directory.GetCurrentDirectory()),
input,
outputDirectory);

using (var stream = input.OpenRead())
{
var result = Compiler.Compile(stream);
using (var stream = input.OpenRead())
{
var result = Compiler.Compile(stream);

OutputCompilationSummary(console, result);
OutputCompilationSummary(console, result);

// TODO: Pull message formatting out to its own object.
foreach (var message in result.Messages.OrderBy(message => message.LineNumber))
{
console.Out.Write($"{thriftFile.RelativePath}({message.LineNumber},{message.StartPosition}-{message.EndPosition}): {message.MessageType} {message.FormattedMessageId}: {message.Message} [{input.FullName}]{Environment.NewLine}");
}
// TODO: Pull message formatting out to its own object.
foreach (var message in result.Messages.OrderBy(message => message.LineNumber))
{
console.Out.Write($"{thriftFile.RelativePath}({message.LineNumber},{message.StartPosition}-{message.EndPosition}): {message.MessageType} {message.FormattedMessageId}: {message.Message} [{input.FullName}]{Environment.NewLine}");
}

if (result.HasErrors)
{
return (int)ExitCode.CompilationFailed;
}
if (result.HasErrors)
{
return (int)ExitCode.CompilationFailed;
}

if (result.Document.ContainsDefinitions)
{
var generatedCode = DocumentGenerator.Generate(result.Document);
if (result.Document.ContainsDefinitions)
{
var generatedCode = DocumentGenerator.Generate(result.Document);

FileWriter.Write(thriftFile, generatedCode);
FileWriter.Write(thriftFile, generatedCode);
}
}
}

return (int)ExitCode.Success;
return (int)ExitCode.Success;
}
catch (Exception exception)
{
console.Out.Write($"Error: {exception}{Environment.NewLine}{Environment.NewLine}");
console.Out.Write($"Thrift.Net has encountered an error. This is a problem with the compiler and not caused by your code.{Environment.NewLine}");
console.Out.Write($"Please help us to resolve this by reporting a new issue here: https://github.com/adamconnelly/Thrift.Net/issues/new?template=issue_template.md. {Environment.NewLine} {Environment.NewLine}");
console.Out.Write($"We welcome contributions, so please feel free to create a PR to resolve the issue.{Environment.NewLine}");
console.Out.Write($"You can find out how to contribute here: https://github.com/adamconnelly/Thrift.Net/blob/main/docs/CONTRIBUTING.md. {Environment.NewLine}");
return (int)ExitCode.UnhandledException;
}
}

private static void OutputCompilationSummary(IConsole console, CompilationResult result)
Expand Down

0 comments on commit 5c71d68

Please sign in to comment.