Related issue: OData#473
Customer wants to customize the output error.
Customer can customize the error serializer to change the output error.
Create the serializer provider as follows:
public class MySerializerProvider : ODataSerializerProvider
{
public MySerializerProvider(IServiceProvider sp) : base(sp)
{
}
public override IODataSerializer GetODataPayloadSerializer(Type type, HttpRequest request)
{
if (type == typeof(ODataError) || type == typeof(SerializableError))
{
return new MyErrorSerializer();
}
return base.GetODataPayloadSerializer(type, request);
}
}
Create MyErrorSerializer as follows:
public class MyErrorSerializer : ODataErrorSerializer
{
public override Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
if (graph is SerializableError error)
{
ODataError oDataError = error.CreateODataError();
oDataError.InnerError = null; // you can change or add more things to the inner error
return base.WriteObjectAsync(oDataError, typeof(ODataError), messageWriter, writeContext);
}
else if (graph is ODataError oDataError)
{
oDataError.InnerError = null; // you can change or add more things to the inner error
return base.WriteObjectAsync(oDataError, typeof(ODataError), messageWriter, writeContext);
}
return base.WriteObjectAsync(graph, type, messageWriter, writeContext);
}
}
Register it in startup
services.AddControllers()
.AddOData(opt => opt.AddRouteComponents("odata", EdmModelBuilder.BuildBookModel(), service => service.AddSingleton<IODataSerializerProvider, MySerializerProvider>()));
http://localhost:1059/ai/students?$filter= status eq 'new'
will get the following error:
{
"error": {
"code": "",
"message": "The query specified in the URI is not valid. The string 'new' is not a valid enumeration type constant.",
"details": []
}
}