 (defresource document [id]
   :allowed-methods #{:get :post}
   :available-media-types +default-media-types+
-  :exists? (fn [ctx]
-             (let [id (Integer/parseInt id)]
-               (when-let [d (m/get-document (get-in ctx [:request ::db]) id)]
-                 {::document d
-                  ::id id})))
+  :malformed? (fn [ctx]
+                (try [false {::id (Integer/parseInt id)}]
+                     (catch NumberFormatException e
+                       true)))
+  :exists? (fn [ctx]
+             (when-let [d (m/get-document (get-in ctx [:request ::db]) (::id ctx))]
+               {::document d}))
   :post-redirect? true
   :location (fn [ctx] (str "/documents/" id))
   :post! (fn [ctx]

                     tagging (get-in request [::config :tagging])]
                 ;; NOTE: auto-tag*! so we don't trigger updates on the notification bus
                 (m/auto-tag*! conn id tagging
                              {:origin origin})
                 {::document (m/get-document conn id)}))))
  :handle-created ::document
  :handle-ok (fn [ctx]

(defresource document-download [id]
  :allowed-methods #{:get}
  :available-media-types ["application/pdf"]
  :malformed? (fn [ctx]
                (try [false {::id (Integer/parseInt id)}]
                     (catch NumberFormatException e
                       true)))
  :exists? (fn [ctx]
             (let [db (get-in ctx [:request ::db])
                   id (Integer/parseInt id)
