2013-06-13 6 views
0

메시지, 소스 및 스택 추적 값이 오버라이드 된 예외를 생성하는 데 도움을 줄 수 있습니까?웹 API의 사용자 정의 예외

HttpResponseMessage에서 httperror를 사용해 보았습니다.하지만 Stacktrace와 Source도 응답 예외에 추가해야합니다. 여기 내 코드는 다음과 같습니다

public Product GetProductById(int id) 
{ 
    var product = products.FirstOrDefault((p) => p.Id == id); 
    HttpResponseMessage errorResponse = null; 

     if (product == null) 
     { 
      //using httperror 
      var message = String.Format("Customer with id: {0} was not found", id); 
      var httpError = new HttpError(message); 
      errorResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError); 
      throw new HttpResponseException(errorResponse); 
     } 

    return product; 
} 

출력 : { Message: "Customer with id: 7 was not found" }

+1

사용자가 스택 추적을 볼 수 없어야합니다. –

답변

0

당신이 HttpConfiguration.IncludeErrorDetailPolicy 속성을 설정하려고 했습니까?

+0

고맙습니다. httperror HttpError 맞춤 설정 문제를 해결했습니다. myCustomError = 새 HttpError ("내 맞춤 오류 메시지") {{ "CustomErrorCode", exp.Source}, { "Stacktrace", exp.StackTrace}}; 새로운 HttpResponseException을 throw합니다 (Request.CreateErrorResponse (HttpStatusCode.BadRequest, myCustomError))); – Manasa