My scenario is this:
- A client application executes a HTTP POST against an endpoint exposed by OpenRasta.
- The body of the request contains an error that causes a problem in the codec – which is a custom implementation of
OpenRasta.Codecs.IMediaTypeReader. This converts a JSON payload to the POCO expected by the handler. - The codec throws an exception that describes the error in a useful way. For example:
Newtonsoft.Json.JsonReaderException: After parsing a value an unexpected character was encountered: ". Line 4, position 5. - The client application receives a HTTP 405 – MethodNotAllowed. The client doesn’t see any of the exception details.
If the codec is modified to catch a JsonReaderException and return Missing.Value, similar to the Implementing a codec wiki, then the client receives a HTTP 500 – Internal Server Error. The body of the response also describes the following exception:
System.InvalidOperationException: The operation is not ready for invocation.
at OpenRasta.OperationModel.MethodBased.MethodBasedOperation.Invoke()
at OpenRasta.OperationModel.Interceptors.OperationWithInterceptors.<Invoke>b__0()
at OpenRasta.OperationModel.Interceptors.OperationWithInterceptors.Invoke()
at OpenRasta.OperationModel.OperationExecutor.Execute(IEnumerable`1 operations)
at OpenRasta.Pipeline.Contributors.OperationInvokerContributor.ExecuteOperations(ICommunicationContext context)
at OpenRasta.Pipeline.PipelineRunner.ExecuteContributor(ICommunicationContext context, ContributorCall call)
How should I modify my application so that:
- The client receives a HTTP 400 Bad Request.
- The client receives a string containing the details of the exception encountered in the codec.
Having found this thread on Google Groups which contains all the answers, my current implementation looks something like this.
Within my implementation of
IConfigurationSource:Then
ErrorCheckingContributorlooks something like this:There’s some things to be aware of with the above:
JsonReaderException, it would also be processed here.context.OperationResulttocontext.ServerErrors– but it doesn’t go through the codec.