Skip to content
Snippets Groups Projects
Unverified Commit 7061a789 authored by Tim Macdonald's avatar Tim Macdonald Committed by GitHub
Browse files

CSV: standardize error handling (#31604)

* Fix a few typos in the middleware

* Standardize format of CSV upload errors

[Fixes #31580]
parent c9e67486
No related branches found
No related tags found
No related merge requests found
......@@ -1224,11 +1224,17 @@ saved later when it is ready."
(api/defendpoint ^:multipart POST "/from-csv"
"Create a table and model populated with the values from the attached CSV. Returns the model ID if successful."
[:as {raw-params :params}]
;; parse-long returns nil with "root", which is what we want anyway
(let [model-id (:id (upload-csv! (parse-long (get raw-params "collection_id"))
(get-in raw-params ["file" :filename])
(get-in raw-params ["file" :tempfile])))]
{:status 200
:body model-id}))
;; parse-long returns nil with "root" as the collection ID, which is what we want anyway
(try
(let [model-id (:id (upload-csv! (parse-long (get raw-params "collection_id"))
(get-in raw-params ["file" :filename])
(get-in raw-params ["file" :tempfile])))]
{:status 200
:body model-id})
(catch Throwable e
{:status (or (-> e ex-data :status-code)
500)
:body {:message (or (ex-message e)
(tru "There was an error uploading the file"))}})))
(api/define-routes)
......@@ -8,7 +8,7 @@
(def +public-exceptions
"Wrap `routes` so any Exception except 404 thrown is just returned as a generic 400, to prevent details from leaking in public
endpoints."
#'mw.exceptions/public-execptions)
#'mw.exceptions/public-exceptions)
(def +message-only-exceptions
"Wrap `routes` so any Exception thrown is just returned as a 400 with only the message from the original
......
......@@ -14,7 +14,7 @@
(declare api-exception-response)
(defn public-execptions
(defn public-exceptions
"Catch any exceptions other than 404 thrown in the request handler body and rethrow a generic 400 exception instead.
This minimizes information available to bad actors when exceptions occur on public endpoints."
[handler]
......@@ -88,7 +88,7 @@
{:status-code 204, :body nil, :headers (mw.security/security-headers)})
(defn catch-api-exceptions
"Middleware that catches API Exceptions and returns them in our normal-style format rather than the Jetty 500
"Middleware (with `[request respond raise]`) that catches API Exceptions and returns them in our normal-style format rather than the Jetty 500
Stacktrace page, which is not so useful for our frontend."
[handler]
(fn [request respond _raise]
......@@ -99,15 +99,15 @@
(defn catch-uncaught-exceptions
"Middleware that catches any unexpected Exceptions that reroutes them thru `raise` where they can be handled
appropriately."
"Middleware (with `[request respond raise]`) that catches any unexpected Exceptions and reroutes them through `raise`
where they can be handled appropriately."
[handler]
(fn [request respond raise]
(try
(handler
request
;; for people that accidentally pass along an Exception, e.g. from qp.async, do the nice thing and route it to
;; the write place for them
;; the right place for them
(fn [response]
((if (instance? Throwable response)
raise
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment