0 people like it.

Extract info from F# Compiler Service exceptions

When you get an exception from the F# Compiler Service, it usually does not print any useful information. But you can look at the private fields of the exception and get some more useful things out of it...

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
// Let's say that creating FsiEvaluator() fails with mysterious
// error from the F# compiler service. We can catch the exception:

let e = 
  try new FSharp.Literate.FsiEvaluator(); failwith "!" with e -> e

// Get the InnerException, which is the actual error from the compiler
let ae = e.InnerException

// And get the values of the private fields!
let opts = Reflection.BindingFlags.NonPublic|||Reflection.BindingFlags.Instance
[ for p in ae.GetType().GetFields(opts) -> p.Name, p.GetValue(ae) ]

// This might give you some more useful information about the
// error (e.g. for `FileNameNotResolved`, you actually get the file name..)
val e : exn

Full name: Script.e
namespace Microsoft.FSharp
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val e : exn
val ae : exn

Full name: Script.ae
property System.Exception.InnerException: exn
val opts : System.Reflection.BindingFlags

Full name: Script.opts
namespace Microsoft.FSharp.Reflection
val p : System.Reflection.FieldInfo
System.Exception.GetType() : System.Type
property System.Reflection.MemberInfo.Name: string
System.Reflection.FieldInfo.GetValue(obj: obj) : obj
Raw view New version

More information

Link:http://fssnip.net/qF
Posted:1 years ago
Author:Tomas Petricek
Tags: f# compiler service , exceptions