Hi all!
I would like to know which is the best way to handle a general, app-level error system; I find myself to reuse this code for each error response:
var responseDto = new ErrorResponse
{
ResponseStatus = new ResponseStatus
{
ErrorCode = "my error code",
Message = "my response message"
}
};
return new HttpError(HttpStatusCode.NotFound, "Not Found")
{
Response = responseDto
};
Should I put it in a class and recall it from each service? Should I write an Interface system like the LogManager does?
Thank you!
It also more importantly populates the HTTP Response Status and description which is most of the time the only thing HTTP clients need to look at. They know a 404 means that what they requested doesn’t exist and can handle it in the most appropriate way for their app.
As for DRY code, treat it just like any other C# code you want to reuse. There’s nothing special about ServiceStack classes, refactor out the common parts into a method just as you would normally, my pref is to use readable extension methods, but use whatever you’re comfortable with.
No it wont have any noticeable impact on performance. Also a static instance is only ever initialized once, however I don’t see any benefit of having a private static instance per class vs just calling a static method:
CustomError.NotFound("my custom message");
But it’s not going to be noticeable either way, although I’d always recommend the least code option where available.