We’re building a blazor was application and found that when using the jsonapiclient to make an unauthenticated request to a [Authenticate] endpoint we don’t get a populated response back, despite having a ResponseStatus property on our dto. Is this expected behaviour and if so what is the best method for capturing the 404 from the client.ApiAsync request?
I was thinking we’d get an error of some kind in the response’s Error property…?
It’s not expected for a failed response not to have a populated Error property, it’s also not the behavior I’m seeing which returns the expected Error Response populated in the Error property:
public class DoesNotExist : IReturn<EmptyResponse> { }
var client = new JsonApiClient("https://blazor-server.jamstacks.net");
var api = await client.ApiAsync(new DoesNotExist());
Console.WriteLine($"{api.Succeeded}: {api.Error?.ErrorCode} {api.Error?.Message}");
Output:
False: NotImplementedException The operation 'DoesNotExist' does not exist for this service
So something’s off, if you can create a stand-alone repro I can investigate.
No we never want access to any confidential info, needs to be a stand-alone repro, ideally starting from the same empty project template your App uses.
I’ve changed approach and tested HelloSecure from the fluxor effect and that generates an Error as expected, but our endpoint doesn’t.
So the out-of-the-box DTO works…
[Route("/hellosecure/{Name}")]
[ValidateIsAuthenticated]
public class HelloSecure : IReturn<HelloResponse>
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
but our DTO doesn’t…
[Route("/systems", "GET")]
[ValidateIsAuthenticated]
public class GetAllSystems : IGet, IReturn<SystemsResponse>
{
}
public class SystemsResponse
{
public List<Types.System> Results { get; set; }
public ResultStats ResultStats { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
Are you able to suggest any areas of further investigation?
Make sure the API is returning the expected error (i.e. non 200) HTTP Response. Try debug into the framework code, seems impossible for it not to return an exception, all it’s doing is calling SendAsync then creating an Error ApiResult from the Exception:
You can try calling SendAsync directly and making sure that throws an Exception.
So if I remove the [ValidateIsAuthenticated] attribute from the DTO and add [Authenticate] to the service method instead HelloSecure then starts exhibiting the same behaviour.
[Route("/hellosecure/{Name}")]
// [ValidateIsAuthenticated]
public class HelloSecure : IReturn<HelloResponse>
{
public string Name { get; set; }
}
public class MyServices : Service
{
public static string AssertName(string Name) => Name.IsNullOrEmpty()
? throw new ArgumentNullException(nameof(Name))
: Name;
public object Any(Hello request) =>
new HelloResponse { Result = $"Hello, {AssertName(request.Name)}!" };
[Authenticate]
public object Any(HelloSecure request) =>
new HelloResponse { Result = $"Hello, {AssertName(request.Name)}!" };
}