我有一個帶有多個REST API的scalatra servlet。爲了記錄目的,我使用after()方法在每個API被調用後打印出返回狀態碼。Scalatra:打印所有API的HTTP狀態代碼
after() {
logger.info("request {} {} returned with status code {}", request.getMethod, request.getRequestURL, response.getStatus.toString)
}
我已經注意到,當一個方法與halt
返回時,狀態碼被正確地打印,但是,當該方法在最後一行返回狀態碼(不halt
),其將被打印的狀態代碼將無論返回的實際狀態如何,始終爲200。
例如:
post("https://stackoverflow.com/users/:user") {
try {
//some logic here...
if(condition)
halt(InternalServerError("DB error")) //this will cause status 500 to be printed in the 'after' method
} catch {
case e: Exception =>
InternalServerError("an unknown error occurred") //this will cause status 200 to be printed in the 'after' method
}
}
用戶回來在這兩種情況下的真實狀態碼(500)。
任何想法爲什麼發生這種情況?這是一個錯誤? 我在scalatra-user郵件列表上發佈了這個問題,但是這個列表似乎很不活躍。
阿里扎
感謝您的詳細解釋。我確實打開了一個錯誤,但還沒有收到消息。我現在的解決方法是用halt()來包裝所有東西。 – Aliza