Skip to content

Commit 40f7f9c

Browse files
Improve error reporting when there are failures
1 parent 83d2951 commit 40f7f9c

2 files changed

Lines changed: 53 additions & 66 deletions

File tree

Analyzer/AnalyzerTool.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ public int Analyze(
7777
catch (Exception e)
7878
{
7979
EraseProgressLine();
80-
Console.Error.WriteLine();
81-
Console.Error.WriteLine($"Error processing file: {file}");
82-
Console.WriteLine($"{e.GetType()}: {e.Message}");
80+
var relativePath = Path.GetRelativePath(path, file);
81+
Console.Error.WriteLine($"Failed to process: {relativePath}");
8382
if (m_Verbose)
84-
Console.WriteLine(e.StackTrace);
83+
{
84+
Console.Error.WriteLine($" Exception: {e.GetType().Name}: {e.Message}");
85+
if (e.InnerException != null)
86+
Console.Error.WriteLine($" Inner: {e.InnerException.Message}");
87+
Console.Error.WriteLine(e.StackTrace);
88+
}
8589
countFailures++;
8690
}
8791
}

Analyzer/SQLite/Parsers/SerializedFileParser.cs

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ public void Parse(string filename)
3535
{
3636
// only init our writer if we are actually parsing a file
3737
m_Writer.Init();
38-
bool successful = ProcessFile(filename, Path.GetDirectoryName(filename));
39-
if (!successful)
40-
{
41-
throw new Exception($"Failed to process file: {filename}");
42-
}
38+
ProcessFile(filename, Path.GetDirectoryName(filename));
4339
}
4440

4541
bool ShouldIgnoreFile(string file)
@@ -71,81 +67,68 @@ bool ShouldIgnoreFile(string file)
7167
".ini", ".config", ".hash", ".md"
7268
};
7369

74-
bool ProcessFile(string file, string rootDirectory)
70+
void ProcessFile(string file, string rootDirectory)
7571
{
76-
bool successful = true;
77-
try
72+
if (IsUnityArchive(file))
7873
{
79-
if (IsUnityArchive(file))
74+
bool archiveHadErrors = false;
75+
using (UnityArchive archive = UnityFileSystem.MountArchive(file, "archive:" + Path.DirectorySeparatorChar))
8076
{
81-
using (UnityArchive archive = UnityFileSystem.MountArchive(file, "archive:" + Path.DirectorySeparatorChar))
82-
{
83-
if (archive == null)
84-
throw new FileLoadException($"Failed to mount archive: {file}");
77+
if (archive == null)
78+
throw new FileLoadException($"Failed to mount archive: {file}");
8579

86-
try
87-
{
88-
var assetBundleName = Path.GetRelativePath(rootDirectory, file);
80+
try
81+
{
82+
var assetBundleName = Path.GetRelativePath(rootDirectory, file);
8983

90-
m_Writer.BeginAssetBundle(assetBundleName, new FileInfo(file).Length);
84+
m_Writer.BeginAssetBundle(assetBundleName, new FileInfo(file).Length);
9185

92-
foreach (var node in archive.Nodes)
86+
foreach (var node in archive.Nodes)
87+
{
88+
if (node.Flags.HasFlag(ArchiveNodeFlags.SerializedFile))
9389
{
94-
if (node.Flags.HasFlag(ArchiveNodeFlags.SerializedFile))
90+
try
91+
{
92+
m_Writer.WriteSerializedFile(node.Path, "archive:/" + node.Path, Path.GetDirectoryName(file));
93+
}
94+
catch (Exception e)
9595
{
96-
try
97-
{
98-
m_Writer.WriteSerializedFile(node.Path, "archive:/" + node.Path, Path.GetDirectoryName(file));
99-
}
100-
catch (Exception e)
101-
{
102-
// the most likely exception here is Microsoft.Data.Sqlite.SqliteException,
103-
// for example 'UNIQUE constraint failed: serialized_files.id'.
104-
// or 'UNIQUE constraint failed: objects.id' which can happen
105-
// if AssetBundles from different builds are being processed by a single call to Analyze
106-
// or if there is a Unity Data Tool bug.
107-
Console.Error.WriteLine($"Error processing {node.Path} in archive {file}");
108-
Console.Error.WriteLine(e.Message);
109-
Console.WriteLine();
110-
111-
// It is possible some files inside an archive will pass and others will fail, to have a partial analyze.
112-
// Overall that is reported as a failure
113-
successful = false;
114-
}
96+
// the most likely exception here is Microsoft.Data.Sqlite.SqliteException,
97+
// for example 'UNIQUE constraint failed: serialized_files.id'.
98+
// or 'UNIQUE constraint failed: objects.id' which can happen
99+
// if AssetBundles from different builds are being processed by a single call to Analyze
100+
// or if there is a Unity Data Tool bug.
101+
Console.Error.WriteLine($"Error processing {node.Path} in archive {file}");
102+
Console.Error.WriteLine(e.Message);
103+
Console.WriteLine();
104+
105+
// It is possible some files inside an archive will pass and others will fail, to have a partial analyze.
106+
// Overall that is reported as a failure
107+
archiveHadErrors = true;
115108
}
116109
}
117110
}
118-
finally
119-
{
120-
m_Writer.EndAssetBundle();
121-
}
111+
}
112+
finally
113+
{
114+
m_Writer.EndAssetBundle();
122115
}
123116
}
124-
else
117+
118+
if (archiveHadErrors)
125119
{
126-
// This isn't a Unity Archive file. Try to open it as a SerializedFile.
127-
// Unfortunately there is no standard file extension, or clear signature at the start of the file,
128-
// to test if it truly is a SerializedFile. So this will process files that are clearly not unity build files,
129-
// and there is a chance for crashes and freezes if the parser misinterprets the file content.
130-
var relativePath = Path.GetRelativePath(rootDirectory, file);
131-
m_Writer.WriteSerializedFile(relativePath, file, Path.GetDirectoryName(file));
120+
throw new Exception("One or more files in the archive failed to process");
132121
}
133122
}
134-
catch (NotSupportedException)
123+
else
135124
{
136-
Console.Error.WriteLine();
137-
//A "failed to load" error will already be logged by the UnityFileSystem library
138-
139-
successful = false;
140-
}
141-
catch (Exception)
142-
{
143-
// Don't log the error here - it will be logged by AnalyzerTool
144-
// Just mark as unsuccessful and let the exception propagate up via Parse()
145-
successful = false;
125+
// This isn't a Unity Archive file. Try to open it as a SerializedFile.
126+
// Unfortunately there is no standard file extension, or clear signature at the start of the file,
127+
// to test if it truly is a SerializedFile. So this will process files that are clearly not unity build files,
128+
// and there is a chance for crashes and freezes if the parser misinterprets the file content.
129+
var relativePath = Path.GetRelativePath(rootDirectory, file);
130+
m_Writer.WriteSerializedFile(relativePath, file, Path.GetDirectoryName(file));
146131
}
147-
148-
return successful;
149132
}
150133

151134
private static bool IsUnityArchive(string filePath)

0 commit comments

Comments
 (0)