0
Web API 2 ODataサービスがあります。問題が発生した場合、ODataErrorを含むHttpResponseExceptionを返しています。エラー応答とODataErrorシリアル化
public ActionResult Post(Customer entity)
{
try
{
Repository.Save(entity);
}
catch (Exception ex)
{
throw ODataException(500, ex);
}
}
protected virtual Exception ODataException(HttpStatusCode code, string message = null, Exception ex = null, IList<ODataErrorDetail> details = null)
{
return ODataException(code, code.ToODataError(message, ex, details, !Request.ShouldIncludeErrorDetail()));
}
protected virtual Exception ODataException(HttpStatusCode code, ODataError error)
{
var message = Request.CreateErrorResponse(code, error);
return new HttpResponseException(message);
}
他の場所で述べたように、Error Handling for ASP.NET OData Web APIは、ODataのV3以来、このように見ている標準のシリアル化された応答が...
{
"error": {
"code": "A custom error code",
"message": {
"lang": "en-us",
"value": "A custom long message for the user."
},
"innererror": {
"trace": [...],
"context": {...}
}
}
}
ただし、変換する簡単な方法があるようには思えませんODataErrorへの応答を返します。
これを行うためのカスタムJSONコンバータを作成できますが、これはODataライブラリでは使用できないということは奇妙に思えます。
:ドキュメントには、逆シリアル化のhere
例を見つけることができます疲れた:-) –