Render exception during Sharp ViewPages

How can we catch and output the exception in html when an expception is raised from a Sharp ViewPage?

The service is raising the exception, a sharp view page is linked in \Views<requestdto>.html. How can I render the exception in the view?

I tried:

{{ ifError |> select: FAIL! { it.Message } }}
{{ lastError |> ifExists |> select: FAIL! { it.Message } }}

The exception is catched by AppHost.OnServiceException(…) method

Thanks

Exceptions in #Script Pages are normally caught on the page and displayed using the Error Handling methods, e.g. I’ll typically have a htmlError at the bottom of my layout page to display errors in a bootstrap styled alert which includes additional detail when in DebugMode:

{{htmlError}}

The Exceptions that are rethrown are FatalExceptions, i.e. NotSupportedException, NotImplementedException, StackOverflowException these are typically bugs in your code that shouldn’t exist and should be fixed immediately, like Syntax errors which would normally be a compile error in compiled code.

I’ve added {{ htmlError }} to the Views/Sample.html page.

This is my service:

public async Task<SampleResponse> Get(Sample request) {
	throw new ArgumentOutOfRangeException();
}

The result of {{ htmlError }} is still empty.
Thanks

All error handling APIs like {{htmlError}} is only for Exceptions raised within the execution of the page, I thought this is what you were asking.

For Service Exceptions, the populated Response DTO is injected in the model of your page as normal, so you’d access like any other variable. e.g:

{{#if model.ResponseStatus}}
    <div class="alert alert-danger">
        {{model.ResponseStatus.ErrorCode}}: {{model.ResponseStatus.Message}}
    </div>
{{/if}}

Note: the are also validation error helpers as seen in the World Validation Script Server example that will display the Response Error message in an alert:

<h3>Error Summary</h3>
{{validationSummary}}

It also lets you exclude a summary message for field validation error messages you’re handling the display of, e.g:

<h3>Error Summary</h3>

<div class="form-group">
    Show error message unless there's a field validation error for `name`:
    {{ 'name' |> validationSummary }}
</div>
<div class="form-group">
    Render `name` input text box along with any `name` field validation errors:
    {{ {id:'name',placeholder:'Name'} 
       |> formInput({label:'Full Name',help:'Your first and last name'}) }}
</div>

Thanks! Adding the ResponseStatus property the response dto answered my question

1 Like