OData / AspNetCoreOData

ASP.NET Core OData: A server library built upon ODataLib and ASP.NET Core
Other
454 stars 161 forks source link

Error message contains inner error details. #473

Open karayanni opened 2 years ago

karayanni commented 2 years ago

Hi team,

I have a simple Odata powered api with a simple endpoint:

when calling the endpoint with an invalid filter query (the enum param new is an invalid status value): http://localhost:1059/ai/students?$filter= status eq 'new'

we receive a BadRequest with an adequate message: "The query specified in the URI is not valid. The string 'new' is not a valid enumeration type constant."

all the above is great, except for one issue, the error response body contains an innererror field containing the details of the exception.

i will provide an example response in the first comment

Is there a way to hide this field from the response? as a customer shouldn't see it

Thanks.

karayanni commented 2 years ago

{ "error": { "code": "", "message": "The query specified in the URI is not valid. The string 'new' is not a valid enumeration type constant.", "details": [], "innererror": { "message": "The string 'new' is not a valid enumeration type constant.", "type": "Microsoft.OData.ODataException", "stacktrace": " at Microsoft.OData.UriParser.MetadataBindingUtils.ConvertToTypeIfNeeded(SingleValueNode source, IEdmTypeReference targetTypeReference)\r\n at Microsoft.OData.UriParser.BinaryOperatorBinder.PromoteOperandTypes(BinaryOperatorKind binaryOperatorKind, SingleValueNode& left, SingleValueNode& right, TypeFacetsPromotionRules facetsPromotionRules)\r\n at Microsoft.OData.UriParser.ODataUriResolver.PromoteBinaryOperandTypes(BinaryOperatorKind binaryOperatorKind, SingleValueNode& leftNode, SingleValueNode& rightNode, IEdmTypeReference& typeReference)\r\n at Microsoft.OData.UriParser.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n at Microsoft.OData.UriParser.MetadataBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n at Microsoft.OData.UriParser.MetadataBinder.Bind(QueryToken token)\r\n at Microsoft.OData.UriParser.FilterBinder.BindFilter(QueryToken filter)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n at Microsoft.AspNetCore.OData.Query.FilterQueryOption.get_FilterClause()\r\n at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at Microsoft.AspNetCore.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNetCore.OData.Query.Validator.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuting(ActionExecutingContext actionExecutingContext)" } } }

xuzhg commented 2 years ago

@karayanni You can customize the OData error serializer to get the expected result.

See detail at: https://github.com/OData/AspNetCoreOData/blob/main/docs/customize_odataerror.md