handle uncaught_exception in clojure

354 views Asked by At

I have a clojure endpoint in my project which basically updates a document in couchdb.

(^{PUT true
       Path "/{id}"
       Produces ["application/json"]
       Consumes ["application/json"]
       ApiOperation {:value "Update" :notes ""}
      }
     method_name [this
                     ^{PathParam "id"} id
                     body]
     (require 'com.xx.x.xx.xx.xx-response)
     (let [doc (json/read-json body)]
       (if-let [valid-doc (validate doc)]
         (try+
          (->>
           (assoc valid-doc :modificationDate (Utilities/getCurrentDate))
           (couch/update-document dbs/xx-db)
           (core/ok-response))
          (catch java.io.IOException ex
              (log/error "line num 197")
           )
          (catch java.lang.Exception ex
            (log/error "line num 200")))
)))

This endpoint throws a 500 uncaught exception when there is a document conflict along with the cause of the exception and this gets logged in the log files. The trace-redirects has sensitive information. I added a catch block to handle this exception [clojure.lang.ExceptionInfo]. It didn't work.

How do I handle this? Is there a way to remove trace-redirects?

Request exception uncaught_exception Status 500 Message An exception was thrown that was not handled by the application or a provider.
clojure.lang.ExceptionInfo: throw+: {:trace-redirects ["https://abc123xyz-dev:separate settled first [email protected]/1111"], :request-time 3899, :request {:path "/sss", :protocol "https", :scheme :https, :data nil, :http-url "https://abc123xyz-dev.cloudant.com/1111", :conn-timeout 6000, :host "abc123xyz-dev.cloudant.com", :follow-redirects true, :request-method :post, :query-string nil, :save-request? true, :anchor nil, :http-req #<HttpPost POST https://abc123xyz-dev.cloudant.com/1111 HTTP/1.1>, :as :json, :uri "/1111", :port -1, :username "abc123xyz-dev", :data-length nil, :server-name "abc123xyz-dev.cloudant.com", :headers {"authorization" "Basic bGl2ZW1vY2hhLWRldjpzZXXBhcmF0ZSBzZXR0bGVkI1ZpcnN0IGRlY111Ww=", "accept-encoding" "gzip, deflate", "content-type" "application/json", "user-agent" "com.ashafa.clutch/0.3.0", "accept" "*/*"}, :socket-timeout 6000, :body-type nil, :server-port nil, :query nil, :password "90t!@"}, :status 400, :headers {"server" "CouchDB/1.0.2 (Erlang OTP/R14B)", "strict-transport-security" "max-age=31536000", "x-couch-request-id" "119df05d59", "content-type" "text/plain;charset=utf-8", "date" "Thu, 18 Jun 2015 17:46:08 GMT", "cache-control" "must-revalidate", "x-content-type-options" "nosniff;", "content-length" "54", "connection" "close"}, :body "{\"error\":\"bad_request\",\"reason\":\"invalid UTF-8 JSON\"}\n"}

Update: Apologies. This error occurs even when a valid json is posted for update. Sorry if that was misleading.

  • Thanks
2

There are 2 answers

0
Daniel Compton On

It looks like you need your try block to be higher up if you want to catch exceptions thrown when reading or validating the JSON.

0
Nicolas Modrzyk On

The

 try+ 

Call should come before the

 read-json 

function call.

So:

      (^{PUT true
             Path "/{id}"
             Produces ["application/json"]
             Consumes ["application/json"]
             ApiOperation {:value "Update" :notes ""}
            }
           method_name [this
                           ^{PathParam "id"} id
                           body]
           (require 'com.xx.x.xx.xx.xx-response)
          (try+
           (let [doc (json/read-json body)]
             (if-let [valid-doc (validate doc)]
                (->>
                 (assoc valid-doc :modificationDate (Utilities/getCurrentDate))
                 (couch/update-document dbs/xx-db)
                 (core/ok-response))
                ))

           (catch java.io.IOException ex
                    (log/error "line num 197")
                 )
                (catch java.lang.Exception ex
                  (log/error "line num 200"))))