From ffe5e4f2cf017bdccd0a3c8b9cebee7b02096d8d Mon Sep 17 00:00:00 2001 From: Matthias Dittrich Date: Thu, 10 May 2018 15:38:00 +0200 Subject: [PATCH] Improve exception message and handling of DocoptException Fix https://github.com/MangelMaxime/fulma-demo/issues/4 Fix https://github.com/fsharp/FAKE/issues/1916 --- .../docopt.fs/Docopt/UsageParser.fs | 2 +- src/app/Fake.Core.Target/Target.fs | 2 +- src/app/Fake.netcore/Program.fs | 28 ++++++++++--------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/app/Fake.Core.CommandLineParsing/docopt.fs/Docopt/UsageParser.fs b/src/app/Fake.Core.CommandLineParsing/docopt.fs/Docopt/UsageParser.fs index 623bc73a676..e2ddfd4bdcf 100644 --- a/src/app/Fake.Core.CommandLineParsing/docopt.fs/Docopt/UsageParser.fs +++ b/src/app/Fake.Core.CommandLineParsing/docopt.fs/Docopt/UsageParser.fs @@ -1,7 +1,7 @@ namespace Fake.Core exception DocoptException of string - with override x.ToString () = sprintf "DocoptException: %s" x.Data0 + with override x.Message = sprintf "DocoptException: %s" x.Data0 namespace Fake.Core.CommandLineParsing diff --git a/src/app/Fake.Core.Target/Target.fs b/src/app/Fake.Core.Target/Target.fs index 440f7ce2065..988e8eb23f9 100644 --- a/src/app/Fake.Core.Target/Target.fs +++ b/src/app/Fake.Core.Target/Target.fs @@ -649,7 +649,7 @@ module Target = | None -> fDefault singleTarget parallelJobs arguments | Choice2Of2 e -> // To ensure exit code. - failwithf "Usage error: %s\n%s" e.Message TargetCli.targetCli + raise <| exn (sprintf "Usage error: %s\n%s" e.Message TargetCli.targetCli, e) /// Runs the command given on the command line or the given target when no target is given let runOrDefault defaultTarget = diff --git a/src/app/Fake.netcore/Program.fs b/src/app/Fake.netcore/Program.fs index fd8c3f66cd2..1e5c19c5dfc 100644 --- a/src/app/Fake.netcore/Program.fs +++ b/src/app/Fake.netcore/Program.fs @@ -78,6 +78,18 @@ type RunArguments = { IsBuild : bool // Did the user call `fake build` or `fake run`? } +let reportExn (verb:VerboseLevel) exn = + use logPaket = + // Required when 'silent' because we use paket API for error printing + if verb = Trace.Silent then + Paket.Logging.event.Publish + |> Observable.subscribe Paket.Logging.traceToConsole + else { new IDisposable with member __.Dispose () = () } + traceError "Script failed" + if Environment.GetEnvironmentVariable "FAKE_DETAILED_ERRORS" = "true" then + Paket.Logging.printErrorExt true true true exn + else Paket.Logging.printErrorExt verb.PrintVerbose verb.PrintVerbose false exn + let runOrBuild (args : RunArguments) = if args.VerboseLevel.PrintVerbose then Trace.log (sprintf "runOrBuild (%A)" args) @@ -147,17 +159,7 @@ let runOrBuild (args : RunArguments) = Fake.Profile.print true sw.Elapsed with | exn -> - use logPaket = - // Required when 'silent' because we use paket API for error printing - if args.VerboseLevel = Trace.Silent then - Paket.Logging.event.Publish - |> Observable.subscribe Paket.Logging.traceToConsole - else { new IDisposable with member __.Dispose () = () } - traceError "Script failed" - if Environment.GetEnvironmentVariable "FAKE_DETAILED_ERRORS" = "true" then - Paket.Logging.printErrorExt true true true exn - else Paket.Logging.printErrorExt args.VerboseLevel.PrintVerbose args.VerboseLevel.PrintVerbose false exn - + reportExn args.VerboseLevel exn //let isKnownException = exn :? FAKEException //if not isKnownException then // sendTeamCityError exn.Message @@ -294,9 +296,9 @@ let main (args:string[]) = parseAction rawResults exitCode <- handleAction verbLevel results with - | :? DocoptException as e -> - printfn "Usage error: %s" e.Message + | exn -> printfn "%s" Cli.fakeUsage + reportExn VerboseLevel.Normal exn exitCode <- 1 Console.OutputEncoding <- encoding #if !NETSTANDARD1_6