Skip to content

How should communications errors be caught? #26

@timbod7

Description

@timbod7

I'm trying out this library (and purescript) for the first time. The only example of which I am aware is this:

https://github.com/slamdata/purescript-halogen/blob/master/examples/ajax/src/Main.purs

Because the AffJaxResponse type has a field containing the http status code, I had assumed that errors would be reflected into this field. If I modify the example, I can see that it is set on success:

handler :: forall eff. String -> E.Event (HalogenEffects (ajax :: AJAX | eff)) Input
handler code = E.yield SetBusy `E.andThen` \_ -> E.async compileAff
  where
  compileAff :: Aff (HalogenEffects (ajax :: AJAX | eff)) Input
  compileAff = do
    result <- post "http://try.purescript.org/compile/text" code
    liftEff $ trace ("status = " ++ show result.status)
    let response = result.response
    return case readProp "js" response <|> readProp "error" response of
      Right js -> SetResult js
      Left _ -> SetResult "Invalid response"

If however, I force an error to occur (eg by making the URL invalid), then the code aborts rather than handles the error, as shown in the javascript console.

POST http://try.purescript.zzz/compile/text net::ERR_NAME_NOT_RESOLVED
...
index.js:12251 Uncaught error in asynchronous code: AJAX request failed: POST http://try.purescript.zzz/compile/text

How should the example code be modified to capture errors such they the can can handled appropriately?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions