Setelah meninjau artikel Penanganan Pengecualian dalam API Web ASP.NET, saya agak bingung kapan harus melempar pengecualian vs mengembalikan respons kesalahan. Saya juga bertanya-tanya apakah mungkin untuk mengubah respons ketika metode Anda mengembalikan model domain tertentu daripada HttpResponseMessage
...
Jadi, untuk rekap di sini adalah pertanyaan saya diikuti oleh beberapa kode dengan case #s:
Pertanyaan
Pertanyaan tentang Kasus # 1
- Haruskah saya selalu menggunakan
HttpResponseMessage
model domain yang konkret, agar pesannya dapat dikustomisasi? - Bisakah pesan dikustomisasi jika Anda mengembalikan model domain konkret?
Pertanyaan tentang Kasus # 2,3,4
- Haruskah saya melempar pengecualian atau mengembalikan respons kesalahan? Jika jawabannya "tergantung", dapatkah Anda memberikan situasi / contoh kapan harus menggunakan satu vs yang lain.
- Apa perbedaan antara melempar
HttpResponseException
vsRequest.CreateErrorResponse
? Output ke klien tampaknya sama ... - Haruskah saya selalu menggunakan
HttpError
untuk "membungkus" pesan respons dalam kesalahan (apakah pengecualian dilemparkan atau respons kesalahan dikembalikan)?
Sampel Kasus
// CASE #1
public Customer Get(string id)
{
var customer = _customerService.GetById(id);
if (customer == null)
{
var notFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(notFoundResponse);
}
//var response = Request.CreateResponse(HttpStatusCode.OK, customer);
//response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return customer;
}
// CASE #2
public HttpResponseMessage Get(string id)
{
var customer = _customerService.GetById(id);
if (customer == null)
{
var notFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(notFoundResponse);
}
var response = Request.CreateResponse(HttpStatusCode.OK, customer);
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return response;
}
// CASE #3
public HttpResponseMessage Get(string id)
{
var customer = _customerService.GetById(id);
if (customer == null)
{
var message = String.Format("customer with id: {0} was not found", id);
var errorResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
throw new HttpResponseException(errorResponse);
}
var response = Request.CreateResponse(HttpStatusCode.OK, customer);
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return response;
}
// CASE #4
public HttpResponseMessage Get(string id)
{
var customer = _customerService.GetById(id);
if (customer == null)
{
var message = String.Format("customer with id: {0} was not found", id);
var httpError = new HttpError(message);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
}
var response = Request.CreateResponse(HttpStatusCode.OK, customer);
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return response;
}
Memperbarui
Untuk membantu lebih lanjut mendemonstrasikan kasus # 2,3,4 cuplikan kode berikut ini menyoroti beberapa opsi yang "dapat terjadi" ketika seorang pelanggan tidak ditemukan ...
if (customer == null)
{
// which of these 4 options is the best strategy for Web API?
// option 1 (throw)
var notFoundMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(notFoundMessage);
// option 2 (throw w/ HttpError)
var message = String.Format("Customer with id: {0} was not found", id);
var httpError = new HttpError(message);
var errorResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
throw new HttpResponseException(errorResponse);
// option 3 (return)
var message = String.Format("Customer with id: {0} was not found", id);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// option 4 (return w/ HttpError)
var message = String.Format("Customer with id: {0} was not found", id);
var httpError = new HttpError(message);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
}