Skip to content

Commit

Permalink
More user friendly error message
Browse files Browse the repository at this point in the history
Instead of HTTP 500, return HTTP 400 and a list of correct parameter
values that the API supports.
  • Loading branch information
mattiuusitalo committed Sep 5, 2023
1 parent 9c15dcd commit 6a1d762
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/laundry/pdf.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns laundry.pdf
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[compojure.api.sweet :as sweet :refer [POST]]
[laundry.machines :as machines :refer [badness-resp]]
[laundry.util :refer [shell-out!]]
Expand All @@ -17,13 +18,20 @@
(proxy-super close)
(io/delete-file path)))))

(def valid-pdf-settings
"Valid values for pdf settings parameter"
#{"/screen" "/ebook" "/printer" "/prepress" "/default"})

(s/defn validate-pdf-settings [pdfsettings :- s/Str]
(when-not (#{"/screen" "/ebook" "/printer" "/prepress" "/default"} pdfsettings) "pdfsettings is not given in correct format"))
(when-not (valid-pdf-settings pdfsettings)
(str "pdfsettings must be one of: " (str/join ", " valid-pdf-settings))))

;; pdf/a converter
(s/defn api-pdf2pdfa [env, tempfile :- java.io.File, dpinum :- s/Int, maxbitmapnum :- s/Int, pdfsettings :- s/Str, pdfaconformancenum :- s/Int]
(if-let [pdfsettings-error (validate-pdf-settings (str pdfsettings))]
(badness-resp pdfsettings-error pdfsettings-error)
(if-let [pdfsettings-error (validate-pdf-settings (str pdfsettings))]
(htresp/content-type
(htresp/bad-request pdfsettings-error)
"text/plain")

(let [in-path (.getAbsolutePath tempfile)
out-path (str (.getAbsolutePath tempfile) ".pdf")
Expand Down

0 comments on commit 6a1d762

Please sign in to comment.