Skip to content

Commit

Permalink
Merge pull request #21 from Amomum/feature_better_errors
Browse files Browse the repository at this point in the history
More informative errors
  • Loading branch information
rumkit committed Jun 25, 2020
2 parents 84cdbfb + 2bf6672 commit 9a07a94
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 84 deletions.
4 changes: 4 additions & 0 deletions Pixie/PatternGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Configuration;

namespace Pixie
{
Expand All @@ -31,6 +32,9 @@ public PatternGenerator(PixelSettings settings)
_backGroundColor = ColorTranslator.FromHtml(_settings.BackgroundColor);
foreach (var i in settings.ColorMapping)
{
if (Colors.ContainsKey(i.Value))
throw new ConfigurationErrorsException($"Config file ColorMapping has several keys {i.Value}");

Colors.Add(i.Value, ColorTranslator.FromHtml(i.Key));
}
}
Expand Down
3 changes: 2 additions & 1 deletion Pixie/PixelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ private byte[] ProcessSymbol(int symbolXStart, int symbolXEnd, int symbolYStart,
}
catch (PixelProcessingException e)
{
throw new PixelProcessingException($"Problem detected while processing pixel at {pixel.X},{pixel.Y}", e);
throw new PixelProcessingException($"Problem detected while processing pixel at x:{pixel.X}, y:{pixel.Y}. " +
$"{e.Message}");
}
}

Expand Down
1 change: 1 addition & 0 deletions Pixie/Pixie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<HintPath>..\packages\CommandLineParser.2.3.0\lib\net45\CommandLine.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
Expand Down
144 changes: 61 additions & 83 deletions Pixie/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
Expand All @@ -19,117 +20,94 @@ class Program
static int Main(string[] args)
{
// Parse command-line arguments
var errorCode = Parser.Default.ParseArguments<GenerateOptions, ParseOptions>(args)
.MapResult(
(GenerateOptions options) => GeneratePattern(options),
(ParseOptions options) => ParseFontImage(options),
errs => ErrorCode.ArgumentsNotParsed);

if(errorCode == ErrorCode.NoError)
ConsoleLogger.WriteMessage($"SUCCESS! \nFile written: \"{_outputFileName}\"", MessageType.Info);
else
try
{
NotifyError(errorCode);
Parser.Default.ParseArguments<GenerateOptions, ParseOptions>(args)
.MapResult(
(GenerateOptions options) => GeneratePattern(options),
(ParseOptions options) => ParseFontImage(options),
// just exit, this usually means that no arguments were given
(errs) =>
{
System.Environment.Exit(1);
return null;
});
}
return (int)errorCode;
catch (Exception e)
{
NotifyError(e);
return 1;
}

ConsoleLogger.WriteMessage($"SUCCESS! \nFile written: \"{_outputFileName}\"", MessageType.Info);

return 0;
}

// Notifies user about error details
private static void NotifyError(ErrorCode errorCode)
private static void NotifyError(Exception e)
{
// todo: add more details
switch (errorCode)
switch (e)
{
case ErrorCode.UknownError:
ConsoleLogger.WriteMessage("There was error, but we have no idea why.", MessageType.Error);
break;
case ErrorCode.ArgumentsMismatch:
ConsoleLogger.WriteMessage("Error parsing arguments. Check command line.", MessageType.Error);
break;
case ErrorCode.FileNotFound:
ConsoleLogger.WriteMessage("File was not found", MessageType.Error);
break;
case ErrorCode.FileParsingError:
ConsoleLogger.WriteMessage("Error parsing file", MessageType.Error);
break;
case ErrorCode.ArgumentsNotParsed:
//do nothings as this usually means that no arguments were passed to command line
break;
case FileNotFoundException _:
ConsoleLogger.WriteMessage($"File not found: {e.Message}", MessageType.Error);
return;
case ArgumentException _:
ConsoleLogger.WriteMessage($"Error parsing file: {e.Message}", MessageType.Error);
return;
default:
ConsoleLogger.WriteMessage($"Unexpected error: {e.Message}", MessageType.Error);
return;
}
}

/// <summary>
/// parses image and write arrays to output file
/// </summary>
/// <param name="options">parsed command line args</param>
/// <returns>error code</returns>
static ErrorCode ParseFontImage(ParseOptions options)
/// <returns>always returns null</returns>
static object ParseFontImage(ParseOptions options)
{
try
if (options.ExcessValue != null)
{
if (options.ExcessValue != null)
return ErrorCode.ArgumentsMismatch;
throw new ArgumentException("Argument mismatch!");
}

Settings = PixelSettings.FromFile(options.PixelSettingsPath);
_outputFileName = options.OutputFileName;
Settings = PixelSettings.FromFile(options.PixelSettingsPath);
_outputFileName = options.OutputFileName;

var bitmap = new Bitmap(Image.FromFile(options.InputFileName));
var mapper = new PixelMapper(bitmap, Settings);
var map = mapper.MapPixels(options.SkipHeaders);
OutputFileFormatter.WriteOutput(map, options.OutputFileName, options.SingleArray, options.ArrayContentOnly);
}
catch (Exception e)
{
switch (e)
{
case FileNotFoundException _:
return ErrorCode.FileNotFound;
case ArgumentException _:
return ErrorCode.FileParsingError;
default:
return ErrorCode.UknownError;
}
}
return ErrorCode.NoError;
var bitmap = new Bitmap(Image.FromFile(options.InputFileName));
var mapper = new PixelMapper(bitmap, Settings);
var map = mapper.MapPixels(options.SkipHeaders);
OutputFileFormatter.WriteOutput(map, options.OutputFileName, options.SingleArray, options.ArrayContentOnly);

return null;
}

/// <summary>
/// Grid pattern generation and writing to file
/// </summary>
/// <param name="options">parsed command line args</param>
/// <returns>error code</returns>
static ErrorCode GeneratePattern(GenerateOptions options)
/// <returns>always returns null</returns>
static object GeneratePattern(GenerateOptions options)
{
try
if (options.ExcessValue != null)
{
if (options.ExcessValue != null)
return ErrorCode.ArgumentsMismatch;
throw new ArgumentException("Argument mismatch!");
}

Settings = PixelSettings.FromFile(options.PixelSettingsPath);
_outputFileName = options.OutputFileName;
Settings = PixelSettings.FromFile(options.PixelSettingsPath);
_outputFileName = options.OutputFileName;

var generator = new PatternGenerator(Settings);
byte[] sampleData = null;
if (options.InputFileName != null)
sampleData = ParseDataFile(options.InputFileName);
var pattern = generator.GeneratePattern(options.PatternWidth,
options.PatternHeight, options.EnumerationStyle, sampleData);
pattern.Save(options.OutputFileName);
}
catch (Exception e)
{
switch (e)
{
case PixelProcessingException _:
ConsoleLogger.WriteMessage(e.Message, MessageType.Error);
return ErrorCode.FileParsingError;
case FileNotFoundException _:
return ErrorCode.FileNotFound;
default:
return ErrorCode.UknownError;
}
}
return ErrorCode.NoError;
var generator = new PatternGenerator(Settings);
byte[] sampleData = null;
if (options.InputFileName != null)
sampleData = ParseDataFile(options.InputFileName);
var pattern = generator.GeneratePattern(options.PatternWidth,
options.PatternHeight, options.EnumerationStyle, sampleData);
pattern.Save(options.OutputFileName);

return null;
}

// Parses file with font hex'es (csv file with hex values e.g. 0xDE, 0xAD, 0xBE, 0xEF ...)
Expand Down

0 comments on commit 9a07a94

Please sign in to comment.