diff --git a/backend/src/app/http/middleware.clj b/backend/src/app/http/middleware.clj index af49f9b09a..e027b5937b 100644 --- a/backend/src/app/http/middleware.clj +++ b/backend/src/app/http/middleware.clj @@ -13,6 +13,7 @@ [app.common.transit :as t] [app.config :as cf] [app.http.errors :as errors] + [app.http.request :as http.request] [app.util.pointer-map :as pmap] [cuerdas.core :as str] [yetti.adapter :as yt] @@ -197,7 +198,8 @@ [handler on-error] (fn [request] (try - (handler request) + (binding [http.request/*current* request] + (handler request)) (catch Throwable cause (on-error cause request))))) diff --git a/backend/src/app/http/request.clj b/backend/src/app/http/request.clj new file mode 100644 index 0000000000..8e6caa1aa2 --- /dev/null +++ b/backend/src/app/http/request.clj @@ -0,0 +1,12 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC + +(ns app.http.request) + +(def ^:dynamic *current* + "Binds to current request" + nil) + diff --git a/backend/src/app/rpc.clj b/backend/src/app/rpc.clj index 02e290bade..39a8727b59 100644 --- a/backend/src/app/rpc.clj +++ b/backend/src/app/rpc.clj @@ -15,7 +15,7 @@ [app.common.time :as ct] [app.config :as cf] [app.db :as db] - [app.http :as-alias http] + [app.http :as http] [app.http.access-token :as actoken] [app.http.client :as-alias http.client] [app.http.session :as session] diff --git a/backend/src/app/rpc/commands/binfile.clj b/backend/src/app/rpc/commands/binfile.clj index 98b2c04193..4c1cc851e4 100644 --- a/backend/src/app/rpc/commands/binfile.clj +++ b/backend/src/app/rpc/commands/binfile.clj @@ -42,10 +42,10 @@ [:include-libraries ::sm/boolean] [:embed-assets ::sm/boolean]]) -(defn stream-export-v1 +(defn- stream-export-v1 [cfg {:keys [file-id include-libraries embed-assets] :as params}] (rph/stream - (fn [_ output-stream] + (fn [output-stream] (try (-> cfg (assoc ::bfc/ids #{file-id}) @@ -57,20 +57,34 @@ :file-id (str file-id) :cause cause)))))) -(defn stream-export-v3 +(defn- stream-export-v3 [cfg {:keys [file-id include-libraries embed-assets] :as params}] - (rph/stream - (fn [_ output-stream] - (try - (-> cfg - (assoc ::bfc/ids #{file-id}) - (assoc ::bfc/embed-assets embed-assets) - (assoc ::bfc/include-libraries include-libraries) - (bf.v3/export-files! output-stream)) - (catch Throwable cause - (l/err :hint "exception on exporting file" - :file-id (str file-id) - :cause cause)))))) + (letfn [(export [output-stream] + ;; (throw (ex-info "kakota" {})) + (-> cfg + (assoc ::bfc/ids #{file-id}) + (assoc ::bfc/embed-assets embed-assets) + (assoc ::bfc/include-libraries include-libraries) + (bf.v3/export-files! output-stream)))] + + (rph/stream export))) + + ;; (with-meta {:file-id file-id + ;; :include-libraries include-libraries + ;; :embed-assets embed-assets})))) + ;; (-> + ;; (rph/stream + ;; (fn [output-stream] + ;; (try + ;; (-> cfg + ;; (assoc ::bfc/ids #{file-id}) + ;; (assoc ::bfc/embed-assets embed-assets) + ;; (assoc ::bfc/include-libraries include-libraries) + ;; (bf.v3/export-files! output-stream)) + ;; (catch Throwable cause + ;; (l/err :hint "exception on exporting file" + ;; :file-id (str file-id) + ;; :cause cause)))))) (sv/defmethod ::export-binfile "Export a penpot file in a binary format." diff --git a/backend/src/app/rpc/helpers.clj b/backend/src/app/rpc/helpers.clj index 5b117b82fd..013e051fa4 100644 --- a/backend/src/app/rpc/helpers.clj +++ b/backend/src/app/rpc/helpers.clj @@ -9,7 +9,11 @@ (:refer-clojure :exclude [with-meta]) (:require [app.common.data.macros :as dm] + [app.common.logging :as l] + [app.config :as cf] [app.http :as-alias http] + [app.http.errors :as errors] + [app.http.request :as http.request] [app.rpc :as-alias rpc] [yetti.response :as yres])) @@ -80,6 +84,15 @@ (update response ::yres/headers assoc "cache-control" val))))) (defn stream - "A convenience allias for yetti.response/stream-body" [f] - (yres/stream-body f)) + (let [request http.request/*current*] + (yres/stream-body + (fn [this response] + (try + (f response) + (catch Throwable cause + (binding [l/*context* (errors/request->context request)] + (l/err :hint "exception streaming response" + :cause cause + ::l/context (meta this))))))))) +