Overriding CreateResponseStatus not firing

Happy new year SS team!

I’ve created a new project using the web template (SS v8, .NET 8), I’ve added this to Configure.AppHost.cs

public override ResponseStatus CreateResponseStatus(Exception ex, object request = null)
{
    return new ResponseStatus("418", "Overridden CreateResponseStatus");
}

I’m throwing an ArgumentException in MyServices

  public object Any(Hello request)
  {
      if (request != null)
          throw new ArgumentException("fd");
      return new HelloResponse { Result = $"Hello, {request.Name}!" };
  }

I’ve added public ResponseStatus ResponseStatus { get; set; } to HelloResponse.

However the custom ResponseStatus is not getting returned (the CreateResponseStatus method isn’t entered at all).

Am I missing something?

Background info: I’m upgrading a project that is using SS v5.5 to SS v8. It is using the ResolveResponseException method to intercept the exception that would eventually be returned. This method was removed in v5.9 so I’m looking for an alternative.

Cheers!

P.S. I can create a repo with the above changes tomorrow if needed.

Happy New Year! :grinning:

This error will show for things like missing routes rather than unhandled service exceptions, but if you want to handle exceptions more broadly, you might want to use a ServiceExceptionHandler via something like the following snippet added to your AppHost’s Configure method:

        ServiceExceptionHandlers.Add((req, request, exception) => new HttpError
        {
            StatusDescription = exception.Message,
            ErrorCode = exception.GetType().Name,
            Status = 418,
        });

If you just use the overridden CreateErrorResponse like in your example and visit a route that doesn’t exist like:

The custom CreateErrorResponse will fire and return as expected.

Hope that helps and let us know if you run into other issues!

1 Like

Thanks I’ll look at returning an HttpError via ServiceExceptionHandlers. I think that will work for me as a replacement to ResolveResponseException.

I see that CreateErrorResponse is used in more specific situations.

Out of interest when should an overridden CreateResponseStatus be used?

Looking through your example, I might have not re-created your reproduction correctly, so if you could share one, I can make sure. The use of CreateResponseStatus should have fired from what I can tell. @mythz has made an update to better ensure it gets fired, as it can depend on what else you have registered in the AppHost.

If you just need to update the ResponseStatus, a better approach would be to not override the CreateStatusResponse and try using the OnExceptionTypeFilter since by default this will get called, and you can add your custom errors to the passed in responseStatus.

To be fair I think my edits crossed with your reply, but I’m interested in overriding anything to do with error handling so it’s all relevant.

I’ve pushed a repo with an overridden CreateResponseStatus.

georgehemmings/CreateResponseStatusOverride (github.com)

The ResponseStatus override wouldn’t help with setting the HTTP status code either, so maybe HandleResponseException is a better hook? However this doesn’t seem to fire for service exceptions either.

Perhaps using the ServiceExceptionHandlers approach will do everything I need. I’m just not sure if ResolveResponseException also applied for exceptions raised outside of services. I’ll experiment today.

Cheers!