Serving custom HTML pages for exceptions

I’ve not had to face this until we implemented an OAuth2 AuthZ Server using SS, and have discovered that we may need to support nicer looking HTML pages when raising exceptions from our services.

It is a common scenario in OAuth2 for a service (i.e. Zapier) to connect to our OAuth2 AuthZ Server and execute the ‘Authorization Flow’, which involves the AuthZ server serving a bunch of HTML pages to login the user and grant them Authorization. Zapier shows these pages to a human being who navigates them.

So we use Razor to serve the standard HTML pages, and everything is fine (as long as there are no exceptions from validation or other things).
However, if an exception is raised (like a validation error) the caller is expecting a text/html response (not a JSON one).

So Zapier shows us a page like this:

I would be good, for just a limited set of service operations, provide a nicer looking HTML rendering of validation errors like this.

How would you go about doing that?

You can provide a Global HTML Error Handler with:

public override void Configure(Container container)
{
    this.GlobalHtmlErrorHttpHandler = new RazorHandler("/oops"),
}

Which will display the /oops.cshtml Razor Page.

For handling a specific page provide a HTML View for that Service, e.g. by adding a /Views/GetAuthorization.cshtml page.

Thanks, is there something special about the name/hierarchy of that View?

It can be in any folder structure in /Views as Request DTO names are unique.

OK, so one last piece.

Lets say I create a Views\GetAuthorization.cshtml page, what would the model for the ViewPage inherit from?

In other words, how do I access the exception information?

It’s a view model, so would need to inherit the Response DTO, which will be empty for Exceptions.

The Razor page base class has a few base.* convenience properties you can use:

  • GetErrorMessage() - Just the Error Message
  • GetErrorStatus() - The Error ResponseStatus DTO
  • GetErrorHtml() - A HTML fragment containing Error Info + StackTrace you can easily embed

great. thank you…