Skip to content

Commit 9a07a94

Browse files
authored
Merge pull request #21 from Amomum/feature_better_errors
More informative errors
2 parents 84cdbfb + 2bf6672 commit 9a07a94

File tree

4 files changed

+68
-84
lines changed

4 files changed

+68
-84
lines changed

Pixie/PatternGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq;
1212
using System.Reflection;
1313
using System.Runtime.InteropServices;
14+
using System.Configuration;
1415

1516
namespace Pixie
1617
{
@@ -31,6 +32,9 @@ public PatternGenerator(PixelSettings settings)
3132
_backGroundColor = ColorTranslator.FromHtml(_settings.BackgroundColor);
3233
foreach (var i in settings.ColorMapping)
3334
{
35+
if (Colors.ContainsKey(i.Value))
36+
throw new ConfigurationErrorsException($"Config file ColorMapping has several keys {i.Value}");
37+
3438
Colors.Add(i.Value, ColorTranslator.FromHtml(i.Key));
3539
}
3640
}

Pixie/PixelMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ private byte[] ProcessSymbol(int symbolXStart, int symbolXEnd, int symbolYStart,
9797
}
9898
catch (PixelProcessingException e)
9999
{
100-
throw new PixelProcessingException($"Problem detected while processing pixel at {pixel.X},{pixel.Y}", e);
100+
throw new PixelProcessingException($"Problem detected while processing pixel at x:{pixel.X}, y:{pixel.Y}. " +
101+
$"{e.Message}");
101102
}
102103
}
103104

Pixie/Pixie.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<HintPath>..\packages\CommandLineParser.2.3.0\lib\net45\CommandLine.dll</HintPath>
4444
</Reference>
4545
<Reference Include="System" />
46+
<Reference Include="System.Configuration" />
4647
<Reference Include="System.Core" />
4748
<Reference Include="System.Drawing" />
4849
<Reference Include="System.Runtime.Serialization" />

Pixie/Program.cs

Lines changed: 61 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.ComponentModel;
34
using System.Drawing;
45
using System.Globalization;
56
using System.IO;
@@ -19,117 +20,94 @@ class Program
1920
static int Main(string[] args)
2021
{
2122
// Parse command-line arguments
22-
var errorCode = Parser.Default.ParseArguments<GenerateOptions, ParseOptions>(args)
23-
.MapResult(
24-
(GenerateOptions options) => GeneratePattern(options),
25-
(ParseOptions options) => ParseFontImage(options),
26-
errs => ErrorCode.ArgumentsNotParsed);
27-
28-
if(errorCode == ErrorCode.NoError)
29-
ConsoleLogger.WriteMessage($"SUCCESS! \nFile written: \"{_outputFileName}\"", MessageType.Info);
30-
else
23+
try
3124
{
32-
NotifyError(errorCode);
25+
Parser.Default.ParseArguments<GenerateOptions, ParseOptions>(args)
26+
.MapResult(
27+
(GenerateOptions options) => GeneratePattern(options),
28+
(ParseOptions options) => ParseFontImage(options),
29+
// just exit, this usually means that no arguments were given
30+
(errs) =>
31+
{
32+
System.Environment.Exit(1);
33+
return null;
34+
});
3335
}
34-
return (int)errorCode;
36+
catch (Exception e)
37+
{
38+
NotifyError(e);
39+
return 1;
40+
}
41+
42+
ConsoleLogger.WriteMessage($"SUCCESS! \nFile written: \"{_outputFileName}\"", MessageType.Info);
43+
44+
return 0;
3545
}
3646

3747
// Notifies user about error details
38-
private static void NotifyError(ErrorCode errorCode)
48+
private static void NotifyError(Exception e)
3949
{
40-
// todo: add more details
41-
switch (errorCode)
50+
switch (e)
4251
{
43-
case ErrorCode.UknownError:
44-
ConsoleLogger.WriteMessage("There was error, but we have no idea why.", MessageType.Error);
45-
break;
46-
case ErrorCode.ArgumentsMismatch:
47-
ConsoleLogger.WriteMessage("Error parsing arguments. Check command line.", MessageType.Error);
48-
break;
49-
case ErrorCode.FileNotFound:
50-
ConsoleLogger.WriteMessage("File was not found", MessageType.Error);
51-
break;
52-
case ErrorCode.FileParsingError:
53-
ConsoleLogger.WriteMessage("Error parsing file", MessageType.Error);
54-
break;
55-
case ErrorCode.ArgumentsNotParsed:
56-
//do nothings as this usually means that no arguments were passed to command line
57-
break;
52+
case FileNotFoundException _:
53+
ConsoleLogger.WriteMessage($"File not found: {e.Message}", MessageType.Error);
54+
return;
55+
case ArgumentException _:
56+
ConsoleLogger.WriteMessage($"Error parsing file: {e.Message}", MessageType.Error);
57+
return;
58+
default:
59+
ConsoleLogger.WriteMessage($"Unexpected error: {e.Message}", MessageType.Error);
60+
return;
5861
}
5962
}
6063

6164
/// <summary>
6265
/// parses image and write arrays to output file
6366
/// </summary>
6467
/// <param name="options">parsed command line args</param>
65-
/// <returns>error code</returns>
66-
static ErrorCode ParseFontImage(ParseOptions options)
68+
/// <returns>always returns null</returns>
69+
static object ParseFontImage(ParseOptions options)
6770
{
68-
try
71+
if (options.ExcessValue != null)
6972
{
70-
if (options.ExcessValue != null)
71-
return ErrorCode.ArgumentsMismatch;
73+
throw new ArgumentException("Argument mismatch!");
74+
}
7275

73-
Settings = PixelSettings.FromFile(options.PixelSettingsPath);
74-
_outputFileName = options.OutputFileName;
76+
Settings = PixelSettings.FromFile(options.PixelSettingsPath);
77+
_outputFileName = options.OutputFileName;
7578

76-
var bitmap = new Bitmap(Image.FromFile(options.InputFileName));
77-
var mapper = new PixelMapper(bitmap, Settings);
78-
var map = mapper.MapPixels(options.SkipHeaders);
79-
OutputFileFormatter.WriteOutput(map, options.OutputFileName, options.SingleArray, options.ArrayContentOnly);
80-
}
81-
catch (Exception e)
82-
{
83-
switch (e)
84-
{
85-
case FileNotFoundException _:
86-
return ErrorCode.FileNotFound;
87-
case ArgumentException _:
88-
return ErrorCode.FileParsingError;
89-
default:
90-
return ErrorCode.UknownError;
91-
}
92-
}
93-
return ErrorCode.NoError;
79+
var bitmap = new Bitmap(Image.FromFile(options.InputFileName));
80+
var mapper = new PixelMapper(bitmap, Settings);
81+
var map = mapper.MapPixels(options.SkipHeaders);
82+
OutputFileFormatter.WriteOutput(map, options.OutputFileName, options.SingleArray, options.ArrayContentOnly);
83+
84+
return null;
9485
}
9586

9687
/// <summary>
9788
/// Grid pattern generation and writing to file
9889
/// </summary>
9990
/// <param name="options">parsed command line args</param>
100-
/// <returns>error code</returns>
101-
static ErrorCode GeneratePattern(GenerateOptions options)
91+
/// <returns>always returns null</returns>
92+
static object GeneratePattern(GenerateOptions options)
10293
{
103-
try
94+
if (options.ExcessValue != null)
10495
{
105-
if (options.ExcessValue != null)
106-
return ErrorCode.ArgumentsMismatch;
96+
throw new ArgumentException("Argument mismatch!");
97+
}
10798

108-
Settings = PixelSettings.FromFile(options.PixelSettingsPath);
109-
_outputFileName = options.OutputFileName;
99+
Settings = PixelSettings.FromFile(options.PixelSettingsPath);
100+
_outputFileName = options.OutputFileName;
110101

111-
var generator = new PatternGenerator(Settings);
112-
byte[] sampleData = null;
113-
if (options.InputFileName != null)
114-
sampleData = ParseDataFile(options.InputFileName);
115-
var pattern = generator.GeneratePattern(options.PatternWidth,
116-
options.PatternHeight, options.EnumerationStyle, sampleData);
117-
pattern.Save(options.OutputFileName);
118-
}
119-
catch (Exception e)
120-
{
121-
switch (e)
122-
{
123-
case PixelProcessingException _:
124-
ConsoleLogger.WriteMessage(e.Message, MessageType.Error);
125-
return ErrorCode.FileParsingError;
126-
case FileNotFoundException _:
127-
return ErrorCode.FileNotFound;
128-
default:
129-
return ErrorCode.UknownError;
130-
}
131-
}
132-
return ErrorCode.NoError;
102+
var generator = new PatternGenerator(Settings);
103+
byte[] sampleData = null;
104+
if (options.InputFileName != null)
105+
sampleData = ParseDataFile(options.InputFileName);
106+
var pattern = generator.GeneratePattern(options.PatternWidth,
107+
options.PatternHeight, options.EnumerationStyle, sampleData);
108+
pattern.Save(options.OutputFileName);
109+
110+
return null;
133111
}
134112

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

0 commit comments

Comments
 (0)