11// Copyright 2020 Cognite AS
22// SPDX-License-Identifier: Apache-2.0
3-
4- namespace Oryx.Pipeline
3+ namespace Oryx
54
65open System
76
87open FSharp.Control .TaskBuilder
98open Oryx
109
1110module Error =
12- /// Handler for protecting the pipeline from exceptions and protocol violations.
13- let protect < 'TContext , ' TSource> ( source : Pipeline < 'TContext , ' TSource>) : Pipeline < 'TContext , 'TSource > =
11+ /// Handler for protecting the HttpHandler from exceptions and protocol violations.
12+ let protect < 'TSource > ( source : HttpHandler < ' TSource>) : HttpHandler < 'TSource > =
1413 fun next ->
1514 let mutable stopped = false
1615
17- { new IAsyncNext < 'TContext , 'TSource> with
16+ { new IHttpNext < 'TSource> with
1817 member _.OnSuccessAsync ( ctx , content ) =
1918 task {
2019 match stopped with
@@ -47,12 +46,12 @@ module Error =
4746 |> source
4847
4948 /// Handler for catching errors and then delegating to the error handler on what to do.
50- let catch < 'TContext , ' TSource>
51- ( errorHandler : 'TContext -> exn -> Pipeline < 'TContext , 'TSource >)
52- ( source : Pipeline < 'TContext , 'TSource >)
53- : Pipeline < 'TContext , 'TSource > =
49+ let catch < 'TSource >
50+ ( errorHandler : HttpContext -> exn -> HttpHandler < 'TSource >)
51+ ( source : HttpHandler < 'TSource >)
52+ : HttpHandler < 'TSource > =
5453 fun next ->
55- { new IAsyncNext < 'TContext , 'TSource> with
54+ { new IHttpNext < 'TSource> with
5655 member _.OnSuccessAsync ( ctx , content ) =
5756 task {
5857 try
@@ -66,7 +65,6 @@ module Error =
6665 match err with
6766 | PanicException error -> return ! next.OnErrorAsync( ctx, error)
6867 | _ -> do ! ( errorHandler ctx err) next
69-
7068 }
7169
7270 member _.OnCancelAsync ( ctx ) = next.OnCancelAsync( ctx) }
@@ -79,22 +77,22 @@ module Error =
7977 | Error
8078 | Panic
8179
82- /// Choose from a list of pipelines to use. The first middleware that succeeds will be used. Handlers will be
80+ /// Choose from a list of HttpHandlers to use. The first middleware that succeeds will be used. Handlers will be
8381 /// tried until one does not produce any error, or a `PanicException`.
84- let choose < 'TContext , ' TSource, 'TResult >
85- ( handlers : ( Pipeline < 'TContext , ' TSource> -> Pipeline < 'TContext , 'TResult>) list )
86- ( source : Pipeline < 'TContext , 'TSource >)
87- : Pipeline < 'TContext , 'TResult > =
82+ let choose < 'TSource , 'TResult >
83+ ( handlers : ( HttpHandler < ' TSource> -> HttpHandler < 'TResult >) list )
84+ ( source : HttpHandler < 'TSource >)
85+ : HttpHandler < 'TResult > =
8886 fun next ->
8987 let exns : ResizeArray < exn > = ResizeArray()
9088
91- { new IAsyncNext < 'TContext , 'TSource> with
89+ { new IHttpNext < 'TSource> with
9290 member _.OnSuccessAsync ( ctx , content ) =
9391 let mutable state = ChooseState.Error
9492
9593 task {
9694 let obv =
97- { new IAsyncNext < 'TContext , 'TResult> with
95+ { new IHttpNext < 'TResult> with
9896 member _.OnSuccessAsync ( ctx , content ) =
9997 task {
10098 exns.Clear() // Clear to avoid buildup of exceptions in streaming scenarios.
@@ -106,10 +104,10 @@ module Error =
106104 member _.OnErrorAsync ( _ , error ) =
107105 task {
108106 match error, state with
109- | PanicException(_) , st when st <> ChooseState.Panic ->
107+ | PanicException _ , st when st <> ChooseState.Panic ->
110108 state <- ChooseState.Panic
111109 return ! next.OnErrorAsync( ctx, error)
112- | SkipException(_) , st when st = ChooseState.NoError ->
110+ | SkipException _ , st when st = ChooseState.NoError ->
113111 // Flag error, but do not record skips.
114112 state <- ChooseState.Error
115113 | _, ChooseState.Panic ->
@@ -122,7 +120,7 @@ module Error =
122120
123121 member _.OnCancelAsync ( ctx ) = next.OnCancelAsync( ctx) }
124122
125- /// Proces handlers until `NoError` or `Panic`.
123+ // Process handlers until `NoError` or `Panic`.
126124 for handler in handlers do
127125 if state = ChooseState.Error then
128126 state <- ChooseState.NoError
@@ -134,9 +132,9 @@ module Error =
134132 ()
135133 | ChooseState.Error, exns when exns.Count > 1 ->
136134 return ! next.OnErrorAsync( ctx, AggregateException( exns))
137- | ChooseState.Error, exns when exns.Count = 1 -> return ! next.OnErrorAsync( ctx, exns. [0 ])
135+ | ChooseState.Error, exns when exns.Count = 1 -> return ! next.OnErrorAsync( ctx, exns[ 0 ])
138136 | ChooseState.Error, _ ->
139- return ! next.OnErrorAsync( ctx, SkipException " Choose: No hander matched" )
137+ return ! next.OnErrorAsync( ctx, SkipException " Choose: No handler matched" )
140138 | ChooseState.NoError, _ -> ()
141139 }
142140
@@ -151,25 +149,19 @@ module Error =
151149 |> source
152150
153151 /// Error handler for forcing error. Use with e.g `req` computational expression if you need to "return" an error.
154- let fail < 'TContext , 'TSource , 'TResult >
155- ( err : Exception )
156- ( source : Pipeline < 'TContext , 'TSource >)
157- : Pipeline < 'TContext , 'TResult > =
152+ let fail < 'TSource , 'TResult > ( err : Exception ) ( source : HttpHandler < 'TSource >) : HttpHandler < 'TResult > =
158153 fun next ->
159- { new IAsyncNext < 'TContext , 'TSource> with
154+ { new IHttpNext < 'TSource> with
160155 member _.OnSuccessAsync ( ctx , content ) = next.OnErrorAsync( ctx, err)
161156 member _.OnErrorAsync ( ctx , exn ) = next.OnErrorAsync( ctx, exn)
162157 member _.OnCancelAsync ( ctx ) = next.OnCancelAsync( ctx) }
163158 |> source
164159
165160 /// Error handler for forcing a panic error. Use with e.g `req` computational expression if you need break out of
166161 /// the any error handling e.g `choose` or `catch`•.
167- let panic < 'TContext , 'TSource , 'TResult >
168- ( err : Exception )
169- ( source : Pipeline < 'TContext , 'TSource >)
170- : Pipeline < 'TContext , 'TResult > =
162+ let panic < 'TSource , 'TResult > ( err : Exception ) ( source : HttpHandler < 'TSource >) : HttpHandler < 'TResult > =
171163 fun next ->
172- { new IAsyncNext < 'TContext , 'TSource> with
164+ { new IHttpNext < 'TSource> with
173165 member _.OnSuccessAsync ( ctx , content ) =
174166 next.OnErrorAsync( ctx, PanicException( err))
175167
0 commit comments