Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add throw function to interrupt pipeline on error #59

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,33 @@ cli { ... }
|> Output.toError // "This is an error!"
```

#### `Output` functions
```fsharp
throwIfErrored: Output -> Output
throw: (Output -> bool) -> Output -> Output
```

`Output.throw` and `Output.throwIfErrored` are assertion functions that if something's not right it will throw an exception.
That is useful for build scripts to stop the execution immediately, here is an example:
```fsharp
cli {
Exec "dotnet"
Arguments [| "build"; "-c"; "Release" |]
WorkingDirectory "src/"
}
|> Command.execute // returns { Id = 123; Text = None; ExitCode = 1; Error = Some "This is an error!" }
|> Output.throwIfErrored // <- Exception thrown!
|> Output.toError
```

or, you can define when to "fail":
```fsharp
cli { ... }
|> Command.execute // returns { Id = 123; Text = "An error occured: ..."; ExitCode = 1; Error = Some "Error detail." }
|> Output.throw (fun output -> output.Text.Contains("error")) // <- Exception thrown!
|> Output.toError
```

#### Printing `Output` fields
There are printing methods in `Output` too:
```fsharp
Expand Down
32 changes: 16 additions & 16 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ RESTRICTION: || (== net6.0) (== net7.0) (== netstandard2.0) (== netstandard2.1)
NUGET
remote: https://api.nuget.org/v3/index.json
FSharp.Core (6.0.7)
FsUnit (5.2)
FSharp.Core (>= 6.0.7)
FsUnit (5.4)
FSharp.Core (>= 5.0.2)
NUnit (>= 3.13.3 < 3.14)
Microsoft.CodeCoverage (17.4.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= net462)) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= net462)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.NET.Test.Sdk (17.4.1)
Microsoft.CodeCoverage (>= 17.4.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= net462)) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= net462)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.TestPlatform.TestHost (>= 17.4.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.NETCore.Platforms (7.0)
Microsoft.TestPlatform.ObjectModel (17.4.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
NuGet.Frameworks (>= 5.11)
Microsoft.CodeCoverage (17.7.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= net462)) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= net462)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.NET.Test.Sdk (17.7.2)
Microsoft.CodeCoverage (>= 17.7.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= net462)) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= net462)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.TestPlatform.TestHost (>= 17.7.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.NETCore.Platforms (7.0.4)
Microsoft.TestPlatform.ObjectModel (17.7.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
NuGet.Frameworks (>= 6.5)
System.Reflection.Metadata (>= 1.6)
Microsoft.TestPlatform.TestHost (17.4.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.TestPlatform.ObjectModel (>= 17.4.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.TestPlatform.TestHost (17.7.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Microsoft.TestPlatform.ObjectModel (>= 17.7.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Newtonsoft.Json (>= 13.0.1) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
NETStandard.Library (2.0.3)
Microsoft.NETCore.Platforms (>= 1.1)
Newtonsoft.Json (13.0.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
NuGet.Frameworks (6.4) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
Newtonsoft.Json (13.0.3) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
NuGet.Frameworks (6.7) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
NUnit (3.13.3)
NETStandard.Library (>= 2.0)
NUnit.Analyzers (3.5)
NUnit3TestAdapter (4.3.1)
NUnit.Analyzers (3.7)
NUnit3TestAdapter (4.5)
System.Collections.Immutable (7.0) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (== net6.0) (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0) (== netstandard2.1)
System.Reflection.Metadata (7.0) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
System.Reflection.Metadata (7.0.2) - restriction: || (== net6.0) (== net7.0) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= netcoreapp3.1))
System.Collections.Immutable (>= 7.0)
System.Runtime.CompilerServices.Unsafe (6.0) - restriction: || (== net6.0) (&& (== net7.0) (== netstandard2.0)) (&& (== net7.0) (== netstandard2.1)) (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (&& (== netstandard2.0) (>= net6.0)) (&& (== netstandard2.0) (>= netcoreapp3.1)) (&& (== netstandard2.1) (>= net6.0)) (&& (== netstandard2.1) (>= netcoreapp3.1))
10 changes: 10 additions & 0 deletions src/Fli/Output.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ module Output =

/// Prints `Error` from `Output`.
let printError = toError >> printfn "%s"

/// Throws exception if given condition in `func` matches.
let throw (func: Output -> bool) (output: Output) =
if output |> func then
failwith $"Execution failed with exit code {output.ExitCode} {output.Error}"

output

/// Throws exception if exit code is not 0.
let throwIfErrored = throw (fun o -> o.ExitCode <> 0)