From 6a1d7629cd986eae6aa51b5c10d7e1a9556da837 Mon Sep 17 00:00:00 2001 From: Matti Uusitalo Date: Tue, 5 Sep 2023 12:08:11 +0300 Subject: [PATCH] More user friendly error message Instead of HTTP 500, return HTTP 400 and a list of correct parameter values that the API supports. --- src/laundry/pdf.clj | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/laundry/pdf.clj b/src/laundry/pdf.clj index eb14a48..4c3253c 100644 --- a/src/laundry/pdf.clj +++ b/src/laundry/pdf.clj @@ -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!]] @@ -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")