fix: catch execution context errors, pedantic packages

This commit is contained in:
orion 2023-12-08 20:36:55 -06:00
parent 540ffb2f78
commit 1a2c4828fd
Signed by: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 47 additions and 12 deletions

View File

@ -7,40 +7,50 @@ package:
- console
- control
- datetime
- dotenv
- effect
- either
- enums
- exceptions
- filterable
- foldable-traversable
- foreign
- identity
- foreign-object
- integers
- js-date
- maybe
- newtype
- node-buffer
- node-event-emitter
- node-path
- node-process
- node-streams
- node-url
- nullable
- ordered-collections
- parallel
- prelude
- record-extra
- record
- simple-json
- spec
- st
- strings
- tailrec
- stringutils
- transformers
- tuples
- typelevel-prelude
- unsafe-coerce
- variant
- web-cssom
- web-dom
- web-html
name: puppeteer
test:
dependencies:
- dotenv
- filterable
- identity
- node-event-emitter
- node-process
- node-streams
- node-url
- spec
- st
- tailrec
main: Test.Main
workspace:
extra_packages: {}
package_set:

View File

@ -2,22 +2,27 @@ module Puppeteer.FFI (mapToRecord, maybeToUndefined, mergeRecords, unsafeMaybeTo
import Prelude
import Control.Monad.Error.Class (catchError, throwError)
import Control.Monad.Except (runExcept)
import Control.Promise (Promise)
import Control.Promise as Promise
import Data.Array as Array
import Data.Either (either)
import Data.Foldable (any)
import Data.FoldableWithIndex (foldlWithIndex)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Data.String as String
import Data.String.Utils (includes) as String
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Effect.Exception as Error
import Foreign (Foreign, unsafeReadTagged)
import Simple.JSON (class WriteForeign)
@ -39,10 +44,30 @@ unsafeMaybeToUndefined :: forall a. Maybe a -> Foreign
unsafeMaybeToUndefined = _maybeToUndefined Nullable.toNullable
promiseToAff :: forall a. Effect (Promise a) -> Aff a
promiseToAff =
promiseToAff work =
let
err e = either (const $ error $ anyToString e) identity
$ runExcept
$ unsafeReadTagged "Error" e
retryErrorsMatching =
[ "execution context destroyed"
]
shouldRetry e =
any
(\retryErr -> String.includes retryErr $ String.trim (Error.message e))
retryErrorsMatching
attempt = do
promise <- liftEffect work
Promise.toAff' err promise
retry e =
if shouldRetry e then do
promise <- liftEffect work
Promise.toAff' err promise
else
throwError e
in
flip bind (Promise.toAff' err) <<< liftEffect
catchError attempt retry