Skip to content
Snippets Groups Projects
Unverified Commit f17f7f29 authored by Chris Truter's avatar Chris Truter Committed by GitHub
Browse files

Render CSV append errors as markdown (#39651)

parent 9fbe6e8d
Branches
Tags
No related merge requests found
......@@ -579,6 +579,19 @@
:type/Date ::date
:type/Text ::text))
(defn- not-blank [s]
(when-not (str/blank? s)
s))
(defn- extra-and-missing-error-markdown [extra missing]
(->> [[(tru "The CSV file contains extra columns that are not in the table:") extra]
[(tru "The CSV file is missing columns that are in the table:") missing]]
(keep (fn [[header columns]]
(when (seq columns)
(str/join "\n" (cons header (map #(str "- " %) columns))))))
(str/join "\n\n")
(not-blank)))
(defn- check-schema
"Throws an exception if:
- the CSV file contains duplicate column names
......@@ -593,18 +606,7 @@
(throw (ex-info (tru "The CSV file contains duplicate column names.")
{:status-code 422})))
(when (or extra missing)
(let [format-columns (fn [cols]
(str/join ", " (map #(str "\"" % "\"") cols)))
error-message (cond
(and extra missing)
(tru "The CSV file contains extra columns that are not in the table: {0}. The CSV file is missing columns that are in the table: {1}."
(format-columns extra) (format-columns missing))
extra
(tru "The CSV file contains extra columns that are not in the table: {0}."
(format-columns extra))
missing
(tru "The CSV file is missing columns that are in the table: {0}."
(format-columns missing)))]
(let [error-message (extra-and-missing-error-markdown extra missing)]
(throw (ex-info error-message {:status-code 422}))))))
(defn- append-csv!*
......
......@@ -1282,7 +1282,7 @@
:data {:status-code 422}}
(catch-ex-info (append-csv-with-defaults! :is-upload false)))))
(testing "The CSV file must not be empty"
(is (= {:message "The CSV file is missing columns that are in the table: \"name\".",
(is (= {:message "The CSV file is missing columns that are in the table:\n- name",
:data {:status-code 422}}
(catch-ex-info (append-csv-with-defaults! :file (csv-file-with [] (mt/random-name)))))))
(testing "Uploads must be supported"
......@@ -1313,19 +1313,34 @@
(rows-for-table table))))
(io/delete-file file)))))))
(defn- trim-lines [s]
(->> (str/split-lines s)
(map str/trim)
(str/join "\n")))
(deftest append-column-mismatch-test
(mt/test-drivers (mt/normal-drivers-with-feature :uploads)
(with-uploads-allowed
(testing "Append should fail if there are extra or missing columns in the CSV file"
(doseq [[csv-rows error-message]
{["_mb_row_id,id,name,extra column one,EXTRA COLUMN TWO"]
"The CSV file contains extra columns that are not in the table: \"extra_column_two\", \"extra_column_one\"."
(trim-lines "The CSV file contains extra columns that are not in the table:
- extra_column_two
- extra_column_one")
[""]
"The CSV file is missing columns that are in the table: \"id\", \"name\"."
(trim-lines "The CSV file is missing columns that are in the table:
- id
- name")
["_mb_row_id,extra 1, extra 2"]
"The CSV file contains extra columns that are not in the table: \"extra_2\", \"extra_1\". The CSV file is missing columns that are in the table: \"id\", \"name\"."}]
(trim-lines "The CSV file contains extra columns that are not in the table:
- extra_2
- extra_1
The CSV file is missing columns that are in the table:
- id
- name")}]
(with-upload-table!
[table (create-upload-table!
{:col->upload-type (ordered-map/ordered-map
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment