【问题标题】:handle uncaught_exception in clojure在 clojure 中处理 uncaught_exception
【发布时间】:2015-06-18 18:22:14
【问题描述】:

我的项目中有一个 clojure 端点,它基本上更新了 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")))
)))

当存在文档冲突以及异常原因时,此端点会引发 500 未捕获异常,并且会记录在日志文件中。跟踪重定向包含敏感信息。 我添加了一个 catch 块来处理这个异常 [clojure.lang.ExceptionInfo]。它没有用。

我该如何处理?有没有办法删除跟踪重定向?

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 deal@abc123xyz-dev.cloudant.com/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"}

更新:道歉。即使发布了有效的 json 以进行更新,也会发生此错误。抱歉,如果这具有误导性。

  • 谢谢

【问题讨论】:

    标签: clojure uncaught-exception clj-http


    【解决方案1】:

    如果您想捕获读取或验证 JSON 时抛出的异常,您似乎需要将 try 块设置得更高。

    【讨论】:

      【解决方案2】:

       try+ 
      

      调用应该在

      之前
       read-json 
      

      函数调用。

      所以:

            (^{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"))))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-28
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-17
        • 2011-07-28
        相关资源
        最近更新 更多