Browse Source

Improve error message on decompiler crashes

pull/1556/head
Daniel Grunwald 6 years ago
parent
commit
4e173cc885
  1. 12
      ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs
  2. 18
      ICSharpCode.Decompiler/DecompilerException.cs

12
ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs

@ -351,10 +351,14 @@ namespace ICSharpCode.Decompiler.CSharp
}, },
delegate (IGrouping<string, TypeDefinitionHandle> file) { delegate (IGrouping<string, TypeDefinitionHandle> file) {
using (StreamWriter w = new StreamWriter(Path.Combine(targetDirectory, file.Key))) { using (StreamWriter w = new StreamWriter(Path.Combine(targetDirectory, file.Key))) {
CSharpDecompiler decompiler = CreateDecompiler(ts); try {
decompiler.CancellationToken = cancellationToken; CSharpDecompiler decompiler = CreateDecompiler(ts);
var syntaxTree = decompiler.DecompileTypes(file.ToArray()); decompiler.CancellationToken = cancellationToken;
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, settings.CSharpFormattingOptions)); var syntaxTree = decompiler.DecompileTypes(file.ToArray());
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, settings.CSharpFormattingOptions));
} catch (Exception innerException) when (!(innerException is OperationCanceledException || innerException is DecompilerException)) {
throw new DecompilerException(module, $"Error decompiling for '{file.Key}'", innerException);
}
} }
}); });
return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(ts, cancellationToken)); return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(ts, cancellationToken));

18
ICSharpCode.Decompiler/DecompilerException.cs

@ -25,6 +25,7 @@ using System.Runtime.InteropServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Security; using System.Security;
using System.Text; using System.Text;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler namespace ICSharpCode.Decompiler
@ -34,20 +35,28 @@ namespace ICSharpCode.Decompiler
/// </summary> /// </summary>
public class DecompilerException : Exception, ISerializable public class DecompilerException : Exception, ISerializable
{ {
public string AssemblyName => Module.AssemblyName; public string AssemblyName => File.Name;
public string FileName => Module.PEFile.FileName; public string FileName => File.FileName;
public IEntity DecompiledEntity { get; } public IEntity DecompiledEntity { get; }
public IModule Module { get; } public IModule Module { get; }
public PEFile File { get; }
public DecompilerException(MetadataModule module, IEntity decompiledEntity, Exception innerException, string message = null) public DecompilerException(MetadataModule module, IEntity decompiledEntity, Exception innerException, string message = null)
: base((message ?? "Error decompiling " + decompiledEntity?.FullName) + Environment.NewLine, innerException) : base(message ?? "Error decompiling " + decompiledEntity?.FullName, innerException)
{ {
this.File = module.PEFile;
this.Module = module; this.Module = module;
this.DecompiledEntity = decompiledEntity; this.DecompiledEntity = decompiledEntity;
} }
public DecompilerException(PEFile file, string message, Exception innerException)
: base(message, innerException)
{
this.File = file;
}
// This constructor is needed for serialization. // This constructor is needed for serialization.
protected DecompilerException(SerializationInfo info, StreamingContext context) : base(info, context) protected DecompilerException(SerializationInfo info, StreamingContext context) : base(info, context)
{ {
@ -71,7 +80,8 @@ namespace ICSharpCode.Decompiler
+ stacktrace; + stacktrace;
exceptionType = GetTypeName(exception); exceptionType = GetTypeName(exception);
} }
return this.Message return this.Message + Environment.NewLine
+ $"in assembly \"{this.FileName}\"" + Environment.NewLine
+ " ---> " + exceptionType + ": " + exception.Message + Environment.NewLine + " ---> " + exceptionType + ": " + exception.Message + Environment.NewLine
+ stacktrace; + stacktrace;
} }

Loading…
Cancel
Save