Skip to content

Conversation

@boxxxie
Copy link
Contributor

@boxxxie boxxxie commented Jul 20, 2022

…s or :ctx from cause

previous: (:ctx data) was used to get ctx, or ctx started from scratch on
error for error-interceptors-chain

however, if a resource has (throw (ex-info "..." {:ctx yada-ctx})) then the
:ctx is likely on the cause (ex-cause e), so (some->> e ex-cause ex-data :ctx) gets the ctx if a user added it to ex-info

this isn't good enough, as if an error is thrown without user doing (try catch (throw (ex-info ...)) then we don't have the ctx anymore, but we
probably want the ctx for error reporting

the below code iterates through each interceptor and returns the last ctx state
before the interceptor chain throws. so now our error-interceptor-chain has
whatever ctx that existed when the error happened.

(let [partial-ctx
      @(d/loop [ctx ctx
                    [interceptor & interceptors-rest] (:interceptor-chain ctx)]
               (let [next-ctx (interceptor ctx)]
                 (if (instance? manifold.deferred.ErrorDeferred next-ctx)
                     ctx
                   (d/recur next-ctx interceptors-rest))))])

…s or :ctx from cause

previous: `(:ctx data)` was used to get `ctx`, or `ctx` started from scratch on
error for error-interceptors-chain

however, if a resource has `(throw (ex-info "..." {:ctx yada-ctx}))` then the
`:ctx` is likely on the cause `(ex-cause e)`, so `(some->> e ex-cause ex-data
:ctx)` gets the `ctx` if a user added it to `ex-info`

this isn't good enough, as if an error is thrown without user doing `(try
catch (throw (ex-info ...))` then we don't have the `ctx` anymore, but we
probably want the `ctx` for error reporting

the below code iterates through each interceptor and returns the last ctx state
before the interceptor chain throws. so now our error-interceptor-chain has
whatever `ctx` that existed when the error happened.

```
(let [partial-ctx
      @(d/loop [ctx ctx
                    [interceptor & interceptors-rest] (:interceptor-chain ctx)]
               (let [next-ctx (interceptor ctx)]
                 (if (instance? manifold.deferred.ErrorDeferred next-ctx)
                     ctx
                   (d/recur next-ctx interceptors-rest))))])
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant