我對我們向客戶返回錯(cuò)誤的方式感到擔(dān)憂。當(dāng)我們收到錯(cuò)誤消息時(shí),是否通過拋出HttpResponseException立即返回錯(cuò)誤消息:public void Post(Customer customer){ if (string.IsNullOrEmpty(customer.Name)) { throw new HttpResponseException("Customer Name cannot be empty", HttpStatusCode.BadRequest) } if (customer.Accounts.Count == 0) { throw new HttpResponseException("Customer does not have any account", HttpStatusCode.BadRequest) }}否則我們會累積所有錯(cuò)誤,然后發(fā)回給客戶:public void Post(Customer customer){ List<string> errors = new List<string>(); if (string.IsNullOrEmpty(customer.Name)) { errors.Add("Customer Name cannot be empty"); } if (customer.Accounts.Count == 0) { errors.Add("Customer does not have any account"); } var responseMessage = new HttpResponseMessage<List<string>>(errors, HttpStatusCode.BadRequest); throw new HttpResponseException(responseMessage);}這只是一個(gè)示例代碼,與驗(yàn)證錯(cuò)誤或服務(wù)器錯(cuò)誤無關(guān),我只想了解最佳實(shí)踐,每種方法的優(yōu)缺點(diǎn)。
- 3 回答
- 0 關(guān)注
- 771 瀏覽
添加回答
舉報(bào)
0/150
提交
取消