Reputation: 16246
So I have modified the todo service that gives me a response.
When I deliberately throw an unhandled error to test ServiceExceptionHandler code below, if I use the default /Backbone.Todos/todo/1?format=json
, it is fine.
But if I use /Backbone.Todos/todo/1?format=xml
, it says:
XML Parsing Error: not well-formed Location: /Backbone.Todos/todo/1 Line Number 1, Column 2:
Here is my code:
public class AppHost : AppHostBase {
public override void Configure(Funq.Container container) {
//Unhandled errors
this.ServiceExceptionHandler = (req, ex) => {
var res = new { //i return a dynamic object here ... and it doesn't work
Result = null as object,
ResponseStatus = new ResponseStatus() {
ErrorCode = "Error",
Message = "Not available",
StackTrace = "Not available"
}
};
return res;
};
//...
}
//....
}
//My normal request/response dtos look like this:
//-----------------------------------------
[Route("/todo/{id}", "GET")] //display req
public class GetTodo : IReturn<TodoResponse> {
public long Id { get; set; }
}
public class TodoResponse : IHasResponseStatus {
public IList<Todo> Result { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
The thing is, when error occured I can't construct a correct type of response without knowing what type it is. I only get incoming Request and Exception as paramaters, but not Response type.
Upvotes: 1
Views: 701
Reputation: 3149
I suspect the error is because XML cannot automatically serialize dynamic objects in .net.
I not entirely sure I know what you are trying to do but you could try using the built in utilities to handle the exception then modify it as you want.
this.ServiceExceptionHandler = (req, ex) =>
{
var responseStatus = ex.ToResponseStatus();
var errorResponse = ServiceStack.ServiceHost.DtoUtils.CreateErrorResponse(req, ex, responseStatus);
return errorResponse;
};
Upvotes: 3