Skip to content

Commit 58f5afd

Browse files
committed
Stop attempting
This package has no opinion about handling errors.
1 parent 1c1244f commit 58f5afd

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/Bucketchain/SimpleAPI/Class.purs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import Bucketchain.SimpleAPI.Body (Body(..), decodeBody)
99
import Bucketchain.SimpleAPI.FreeT.Class (class Transformable, transform)
1010
import Bucketchain.SimpleAPI.Proc (Proc, context, runProc)
1111
import Bucketchain.SimpleAPI.RawData (RawData)
12-
import Bucketchain.SimpleAPI.Response (Response, fromResponses, invalidRequestResponse, unauthorizedResponse, errorResponse)
12+
import Bucketchain.SimpleAPI.Response (Response, fromResponses, invalidRequestResponse, unauthorizedResponse)
1313
import Bucketchain.SimpleAPI.Response.Class (class Respondable, toResponse)
1414
import Control.Monad.Free.Trans (FreeT, runFreeT)
1515
import Control.Parallel (parTraverse)
1616
import Data.Either (Either(..))
1717
import Data.Maybe (Maybe(..))
1818
import Data.String (drop)
1919
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
20-
import Effect.Aff (Aff, attempt)
20+
import Effect.Aff (Aff)
2121
import Foreign (MultipleErrors)
2222
import Prim.Row (class Cons)
2323
import Record.Unsafe (unsafeGet)
@@ -36,11 +36,8 @@ class ServableList ex (l :: RowList) (r :: # Type) | l -> r where
3636
serveList :: RLProxy l -> Record r -> ex -> RawData -> Aff (Maybe Response)
3737

3838
instance servableProc :: (Respondable a) => Servable ex (Proc ex a) where
39-
serve server extraData rawData = do
40-
result <- attempt $ runProc server $ context extraData rawData
41-
case result of
42-
Left _ -> pure $ Just errorResponse
43-
Right x -> pure $ Just $ toResponse x
39+
serve server extraData rawData =
40+
(runProc server $ context extraData rawData) <#> toResponse >>> Just
4441

4542
instance servableFreeT :: (Transformable ex f, Respondable a) => Servable ex (FreeT f (Proc ex) a) where
4643
serve server extraData rawData = serve (runFreeT transform server) extraData rawData

src/Bucketchain/SimpleAPI/Response.purs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module Bucketchain.SimpleAPI.Response
99
, fromResponses
1010
, invalidRequestResponse
1111
, unauthorizedResponse
12-
, errorResponse
1312
) where
1413

1514
import Prelude
@@ -64,9 +63,5 @@ invalidRequestResponse = response defaultHeaders 400 $ write { message: "Request
6463
unauthorizedResponse :: Response
6564
unauthorizedResponse = response defaultHeaders 401 $ write { message: "Unauthorized" }
6665

67-
-- | This is for internal. Do not use it.
68-
errorResponse :: Response
69-
errorResponse = response defaultHeaders 500 $ write { message: "Internal server error" }
70-
7166
defaultHeaders :: Headers
7267
defaultHeaders = singleton "Content-Type" "application/json; charset=utf-8"

test/Main.purs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ main = do
3434
errorTest
3535
freeTTest
3636
batchTest
37+
batchErrorTest
3738
where
3839
opts =
3940
{ hostname: "localhost"
@@ -175,7 +176,7 @@ batchTest = do
175176
res <- requestWithBody opts batchBody
176177
body <- convertToString $ C.responseAsStream res
177178
liftEffect do
178-
assert $ body == "[{\"status\":503,\"headers\":{\"X-Custom\":\"CustomValue2\",\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"core\":[\"This is error test\"]}},{\"status\":201,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"name\":\"Other Item 1\"}},{\"status\":200,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"name\":\"authuser\"}},{\"status\":404,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":null},{\"status\":500,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"message\":\"Internal server error\"}}]"
179+
assert $ body == "[{\"status\":503,\"headers\":{\"X-Custom\":\"CustomValue2\",\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"core\":[\"This is error test\"]}},{\"status\":201,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"name\":\"Other Item 1\"}},{\"status\":200,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":{\"name\":\"authuser\"}},{\"status\":404,\"headers\":{\"Content-Type\":\"application/json; charset=utf-8\"},\"body\":null}]"
179180
assert $ C.statusCode res == 200
180181
where
181182
opts = C.port := 3000
@@ -188,5 +189,22 @@ batchTest = do
188189
, Tuple "X-Test-Auth" "authuser"
189190
]
190191

192+
batchErrorTest :: Aff Unit
193+
batchErrorTest = do
194+
res <- requestWithBody opts batchErrorBody
195+
liftEffect $ assert $ C.statusCode res == 500
196+
where
197+
opts = C.port := 3000
198+
<> C.method := "POST"
199+
<> C.path := "/batch"
200+
<> C.headers := headers
201+
headers =
202+
C.RequestHeaders $ fromFoldable
203+
[ Tuple "Content-Type" "application/json"
204+
]
205+
191206
batchBody :: String
192-
batchBody = "[{\"path\":\"/failureTest\"},{\"path\":\"/bodyTest\",\"body\":{\"name\":\"Other Item 1\"}},{\"path\":\"/authTest\"},{\"path\":\"/notFound\"},{\"path\":\"/errorTest\"}]"
207+
batchBody = "[{\"path\":\"/failureTest\"},{\"path\":\"/bodyTest\",\"body\":{\"name\":\"Other Item 1\"}},{\"path\":\"/authTest\"},{\"path\":\"/notFound\"}]"
208+
209+
batchErrorBody :: String
210+
batchErrorBody = "[{\"path\":\"/bodyTest\",\"body\":{\"name\":\"Other Item 1\"}},{\"path\":\"/errorTest\"}]"

0 commit comments

Comments
 (0)