I get a 500 page instead of a ResponseStatus from exceptions within ServiceStack Service - servicestack

Using ServiceStack 4.0.33 and given the following simplified DTOs...
[Route("/products")]
[Route("/products/{Id}")]
public class Product : IReturn<ProductResponse>
{
[PrimaryKey]
public string Id { get; set; }
public string Description { get; set; }
}
public class ProductResponse
{
public Product Product { get; set; }
}
with the following simplified service...
public class ProductService : Service
{
public object Post(Product product)
{
Db.Insert<Product>(product);
return new ProductResponse() { Product = product };
}
}
and calling it via this in my ProductsController
using (var productService = ResolveService<ProductService>())
{
var result = productService.Post(product);
if (result.IsErrorResponse())
return View(product);
else
return RedirectToAction("Index");
}
If I try to post a new Product with an intentional duplicate primary key, I get a 500 error HTML-style page instead of the ResponseStatus getting populated and returned...
I've seen lots of different StackOverflow posts about different reasons that ResponseStatus won't get populated, but I've tried several things to no avail. Am I missing something (hopefully simple)?

Where do you define the behavior that a duplicate key would be handled differently than HTTP 500. What other response would you expect? ResponseStatus can contain HTTP 500 which merely indicates a server-side error.
You have to specifically define that you want to handle the error differently and if so what type of error you want (which error code, which message) as explained here.
For instance:
public object Get(User request)
{
throw HttpError.NotFound("User {0} does not exist".Fmt(request.Name));
}
Check out the ServiceStack tutorial for more.

The error page you're getting is a custom ASP.NET Error page which hijacks any custom 500 HTTP error responses returned by your ASP.NET web application.
It can be disabled with:
<system.web>
...
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
Here's another alternative of disabling IIS errors by setting TrySkipIisCustomErrors = true which in ServiceStack can be set with:
GlobalRequestFilters.Add((req, res, dto) =>
{
((HttpResponse)res.OriginalResponse).TrySkipIisCustomErrors = true;
});

Related

SwaggerRequestExample attribute does not work in ASP.NET MVC 5 (.NET Framework 4.5.2)

I am toying with Swashbuckle.Examples package (3.10.0) in an ASP.NET MVC project. However, I cannot make request examples appear within the UI.
Configuration (SwaggerConfig.cs)
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c => {
c.SingleApiVersion("v1", "TestApp.Web");
c.IncludeXmlComments(string.Format(#"{0}\bin\TestApp.Web.xml", System.AppDomain.CurrentDomain.BaseDirectory));
c.OperationFilter<ExamplesOperationFilter>();
c.OperationFilter<DescriptionOperationFilter>();
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
})
.EnableSwaggerUi(c => { });
}
Request example classes
public class EchoRequestExample : IExamplesProvider
{
public object GetExamples()
{
return new EchoInput { Value = 7 } ;
}
}
public class EchoInput
{
public int Value { get; set; }
}
Action
[HttpGet]
[Route("Echo")]
[CustomApiAuthorize]
[SwaggerRequestExample(typeof(EchoInput), typeof(EchoRequestExample))]
[ResponseType(typeof(EchoServiceModel))]
public HttpResponseMessage Echo([FromUri] EchoInput model)
{
var ret = new EchoServiceModel
{
Username = RequestContext.Principal.Identity.Name,
Value = value
};
return Request.CreateResponse(HttpStatusCode.OK, ret);
}
Swagger UI shows xml comments and output metadata (model and an example containing default values), but shows no request example. I attached to process and EchoRequestExample.GetExamples is not hit.
Question: How to make SwaggerRequestExample attribute work in ASP.NET MVC 5?
Note: Windows identity is used for authorization.
I received an answer from library owner here:
Swagger request examples can only set on [HttpPost] actions
It is not clear if this is a design choice or just a limitation, as I find [HttpGet] examples also relevant.
I know the feeling, lot's of overhead just for an example, I struggle with this for a while, so I created my own fork of swashbuckle, and after unsuccessful attempts to merge my ideas I ended up detaching and renaming my project and pushed to nuget, here it is: Swagger-Net
An example like that will be:
[SwaggerExample("id", "123456")]
public IHttpActionResult GetById(int id)
{
Here the full code for that: Swagger_Test/Controllers/IHttpActionResultController.cs#L26
Wondering how that looks like on the Swagger-UI, here it is:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=IHttpActionResult#/IHttpActionResult/IHttpActionResult_GetById

Impossible to show Custom Error page with Nancy on OWIN

I have a website using Nancy which is hosted using OWIN.
In my Startup.cs file I define the PassThroughOptions as follows:
public void Configuration(IAppBuilder app)
{
app.UseNancy(o => {
o.PassThroughWhenStatusCodesAre(
HttpStatusCode.NotFound,
HttpStatusCode.InternalServerError
);
o.Bootstrapper = new Bootstrapper();
});
app.UseStageMarker(PipelineStage.MapHandler);
}
I need to pass-through the NotFound requests, so that things like my bundled .less files or miniprofiler-results or static files in the root of my site (robots.txt or sitemap.xml) work.
I also have a custom StatusCodeHandler for the 404 code, which also checks a custom header to distinguish between static files (or .less bundles/miniprofiler) and actual stuff that is not found in my modules' methods.
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
Log.Warn("Not found: " + context.Request.Url);
base.Handle(statusCode, context, "Errors/NotFound");
}
This handler then should actually show the error page.
protected void Handle(HttpStatusCode statusCode, NancyContext context, string view)
{
var response = new Negotiator(context)
.WithModel(GetErrorModel(context))
.WithStatusCode(statusCode)
.WithView(view);
context.Response = responseNegotiator.NegotiateResponse(response, context);
}
But the error page is never shown. The request is processed three times and eventually the default IIS error page is shown (using errorMode="Custom" for httpErrors) or simply a white page (using existingResponse="PassThrough" for httpErrors).
Is there any way to display something so simple as a custom error page when hosting a Nancy website on OWIN?
What you've got there looks good, it looks like you've be using the Hosting Nancy with Owin docs.
Here's what works for me:
The Startup.cs (required for Owin): (We've both coded the configuration function differently, you're just using the extension helper while I'm not. Same result. This is in my App.Web project.)
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseNancy(options =>
{
options.Bootstrapper = new BootStrapper();
options.PerformPassThrough = context => context.Response.StatusCode == HttpStatusCode.NotFound;
});
app.UseStageMarker(PipelineStage.MapHandler);
}
}
404 handler: (As per the docs, doesn't matter where this is in the project, by implementing IStatusCodeHandler it'll be automatically picked up by Nancy This is in my App.WebApi project with other module classes.)
public class StatusCode404Handler : IStatusCodeHandler
{
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
return statusCode == HttpStatusCode.NotFound;
}
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
var response = new GenericFileResponse("statuspages/404.html", "text/html")
{
StatusCode = statusCode
};
context.Response = response;
}
}
The 'statuspages' folder in my App.Web project:
Check this SO post for a comparison of using GenericFileReponse or ViewRenderer (How to display my 404 page in Nancy?).

Custom Error Pages with ASP.NET MVC 5 and Elmah

I am trying make work custom error page in asp mvc 5 but for some strange reason at moment to test my page, from elmah i am loging two errors ( the real error what i am testing and a error related with error page not found:
The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/HotTowel/Error.aspx
~/Views/HotTowel/Error.ascx
~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx
~/Views/HotTowel/Error.cshtml
~/Views/HotTowel/Error.vbhtml
~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml
I was looking into this url http://doingthedishes.com/2011/09/10/custom-errors-mvc-3-elmah.html, where the author had the same issue but with asp.net mvc 3. After read it, I tried remove the call to HandleErrorAttribute:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
}
}
But the issue is still there: i can see my custom page but asp.net mvc is throwing two exceptions.
Any help?
the solution is rewrite a class derived from HandleErrorAttribute ?
like this post: keep getting The view "Error" not found when using Elmah and asp.net mvc 4 ?
You can do the following from ELMAH.MVC 2.0.2 is out:
Set disableHandleErrorFilter to true:
<add key="elmah.mvc.disableHandleErrorFilter" value="true" />
Remove filters.Add(new HandleErrorAttribute()); from FilterConfig class:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// filters.Add(new HandleErrorAttribute()); // <-- comment out
}
}
Here's a possible solution for you. I typically override the OnException method in a base controller class. filterContext.HttpContext.IsCustomErrorEnabled checks <customErrors> in the web.config. The showVerboseErrors variable is derived from a setting in the web.config.
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
//trigger elmah
Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
//get the last elmah error
var errorList = new List<ErrorLogEntry>();
Elmah.ErrorLog.GetDefault(filterContext.HttpContext.ApplicationInstance.Context).GetErrors(0, 1, errorList);
var error = errorList.LastOrDefault();
//return the custom error page
filterContext.Result = new ViewResult
{
ViewName = "~/Views/Shared/Error.cshtml",
ViewData = new ViewDataDictionary() {
{ "ErrorDetails", showVerboseErrors && error != null ? filterContext.Exception.Message : null },
{ "ErrorId", error != null ? error.Id : null }
}
};
//stop further error processing
filterContext.ExceptionHandled = true;
}
else
{
base.OnException(filterContext);
}
}

Intermittent Validation Problems using ServiceSttack

I'm using ServiceStack to build an API and at the same time, I'm using the plugin that allows Razor views to return html to browsers.
I have validation set up and configured correctly. I know this because I get the validation messages on the corresponding Razor view and the messages are accurate. However, if I modify the Razor view at all (and by "at all" I mean something as simple as adding a line break and then immediately deleting it), I get a 500 error accompanied by a blank page.
Other times, while in the process of simply refreshing the page to review styling of the Razor view, the validation simply returns a blank page with the same non-useful 500 error. And of course, if I remove the validation, the Razor view renders just fine 100% of the time.
What must I do to have validation working at all times? My code is straight forward and matches everything that I've been able to read in the Docs. Namely, both the response and the requests are in the same namespace and the validator is coded to the request.
Here is the request DTO
namespace MyServer.DTO
{
[Validator(typeof(SignUpValidator))]
[Route("SignUp")]
public class SignUp : IReturn<SignUpResponse>
{
public string UserName { get; set; }
public string Email { get; set; }
public string EmailConfirm { get; set; }
public string Password { get; set; }
public string PasswordConfirm { get; set; }
public int UserId { get; set; }
}
}
Here is the corresponding validator
namespace MyServer.DTO
{
public class SignUpValidator : AbstractValidator<SignUp>
{
public SignUpValidator()
{
RuleSet(ApplyTo.Post, () =>
{
RuleFor(e => e.UserName).NotEmpty();
RuleFor(e => e.Email).NotEmpty();
RuleFor(e => e.EmailConfirm).NotEmpty();
RuleFor(e => e.Password).NotEmpty();
RuleFor(e => e.PasswordConfirm).NotEmpty();
}
);
}
}
}
Here is the response
namespace MyServer.DTO
{
public class SignUpResponse
{
bool DidSucceed { get; set; }
int NewUserId { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
}
And lastly, here is the code that configures the validation plugin
Plugins.Add(new ValidationFeature());
Container.RegisterValidators(typeof(SignUpService).Assembly);
As you can see, everything is pretty vanilla and by the book, but this setup is very fragile for some reason. Any modification to the corresponding Razor view and I get the aforementioned errors. Then I have to recompile repeatedly until it works again.
I should also mention that, if I use the REST Console (google chrome extension thingy) to test this, I get the following results when posting to the exact same URI:
Content-Type set to : application/json - Everything works as intended. 400 Response with the errors listed in the response body.
Content-Type set to : application/html - Consistently breaks. 500 Response with no data in the response body.
There must be something that I'm missing.
Thanks so much for your time and I would appreciate any help.
Thanks again.

Inconsistent ServiceStack exception handling

I have a simple service built with ServiceStack
public class GetContactMasterDataService : IService<GetContactMasterData>
{
public object Execute(GetContactMasterData getContactMasterData)
{
return ContactApi.FetchContactMasterData();
}
}
In a different namespace:
public class GetContactMasterData
{
}
public class GetContactMasterDataResponse
{
public ResponseStatus ResponseStatus { get; set; }
}
public static GetContactMasterDataResponse FetchContactMasterData()
{
throw new ApplicationException("CRASH");
}
When I send a JSON request I correctly get:
{
"ResponseStatus":{
"ErrorCode":"ApplicationException",
"Message":"CRASH",
}
}
When I send a soap12 request with soapUI, I get the typical yellow screen of death
<html>
<head>
<title>CRASH</title>
...
<h2> <i>CRASH</i> </h2></span>
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
...
<b> Exception Details: </b>System.ApplicationException: CRASH<br><br>
Is this the expected behavior? How can I get a neatly serialized ResponseStatus similar to the JSON response.
Thanks in advance.
The HTML error page you get doesn't looks like it's coming from ServiceStack, check to see if your website has something that could be hijacking the errors with its own page, e.g: <customErrors />.
The correct behavior for SOAP endpoints is to throw a SOAP fault which if you're using either the Soap11ServiceClient or Soap12ServiceClient generic service clients will be converted to a WebServiceException as seen in this Integration test:
var client = new Soap12ServiceClient(ServiceClientBaseUri);
try
{
var response = client.Send<AlwaysThrowsResponse>(
new AlwaysThrows { Value = TestString });
Assert.Fail("Should throw HTTP errors");
}
catch (WebServiceException webEx)
{
var response = (AlwaysThrowsResponse) webEx.ResponseDto;
var expectedError = AlwaysThrowsService.GetErrorMessage(TestString);
Assert.That(response.ResponseStatus.ErrorCode,
Is.EqualTo(typeof(NotImplementedException).Name));
Assert.That(response.ResponseStatus.Message,
Is.EqualTo(expectedError));
}

Resources