kontent-ai / delivery-sdk-net

Kontent.ai Delivery .NET SDK
https://www.nuget.org/packages/Kontent.Ai.Delivery
MIT License
32 stars 43 forks source link

GetItemsAsync and GetTaxonomiesAsync return "Object reference not set to an instance of an object" rather than a helpful error message, when the Project ID or ApiKey is incorrect #326

Closed DeniseG001 closed 2 years ago

DeniseG001 commented 2 years ago

Brief bug description

GetItemsAsync and GetTaxonomiesAsync both return {"Object reference not set to an instance of an object"} error message when the project ID or ApiKey is incorrect. This makes error handling much more difficult as it does not explain the cause.

Repro steps

  1. Create a delivery client with an invalid project id (e.g. if the project has just been re-cloned but we have forgotten to update the settings in our code)

  2. Call GetTaxonomiesAsync or GetItemsAsync

e.g. response = await deliveryClient.GetTaxonomiesAsync(new EqualsFilter("system.codename", taxonomyGroupName));

Expected behavior

A more helpful error message, like the one below returned from GetItemAsync in the same situation:

"APIResponse returned error code: '3' APIResponse returned message: 'Missing or invalid access token. Please include the valid access token value in the Authorization header field as an HTTP bearer authorization scheme.'"

Test environment

Additional context

GetItemsAsync error message and stack trace:

{"Object reference not set to an instance of an object."} System.NullReferenceException

Source: "Kentico.Kontent.Delivery"

   at Kentico.Kontent.Delivery.DeliveryClient.<GetItemsAsync>d__11`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Services.Content.Kentico.ItemService.AgentPortal.AgentPortalCoreKenticoClient.<GetItemsAsync>d__2`1.MoveNext() in D:\Projects\Services.Content\Data\Services.Content.Kentico\ItemService\AgentPortal\AgentPortalCoreKenticoClient.cs:line 113

GetTaxonomiesAsync error message and stack trace:

{"Object reference not set to an instance of an object."} System.NullReferenceException

Source: "Kentico.Kontent.Delivery"

StackTrace:

   at Kentico.Kontent.Delivery.DeliveryClient.<GetTaxonomiesAsync>d__17.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Services.Content.Kentico.ItemService.AgentPortal.AgentPortalCoreKenticoClient.<GetTaxonomyGroup>d__6.MoveNext() in D:\Projects\Services.Content\Data\Services.Content.Kentico\ItemService\AgentPortal\AgentPortalCoreKenticoClient.cs:line 220
MiroKentico commented 2 years ago

Hello @DeniseG001,

thank you for the report, I was able to reproduce problems you found out. From brief debug it really seems that parts of SDK code need to be fixed.

I understand that thrown null exception is "too generic" to handle and in ideal implementation situation, it should newer be thrown by our SDK. So your expectations are definitely sensible. We will fix SDK so it behaves as you expected.

mattnield commented 2 years ago

@MiroKentico , I've just run into (possibly) the same issue, so thought I'd add it here just in case it helps. It appears to affect more exceptions than just invalid keys.

In my case though, it's not that the API keys are wrong, but just that I was trying to retrieve too much content. My code was:

var client = DeliveryClientBuilder
  .WithOptions(builder => builder
    .WithProjectId(PROJECT_ID)
    .UsePreviewApi(PREVIEW_API_KEY)
    .Build()
  )
  .WithTypeProvider(new KontentTypeProvider())
  .Build();

var res = await client.GetItemsAsync<DealerDetails>();

which would return:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Kentico.Kontent.Delivery.DeliveryClient.GetItemsAsync[T](IEnumerable`1 parameters)
   at Program.<Main>$(String[] args) in /Users/mattnield/Projects/kontent-serialize/Program.cs:line 31
   at Program.<Main>(String[] args)

After checking the API with the same keys in Postman, the following was returned:

{
    "message": "Your request reached the maximum response size of 2001 content items. To make a successful request, use content filtering and the 'limit' parameter to retrieve fewer content items. If you need to retrieve all content items from your project, we recommend using the items-feed endpoint. Find more details in https://kontent.ai/learn/dapi-response-size.",
    "request_id": "01d19e07d4bfa23c",
    "error_code": 6,
    "specific_code": 0
}

Adding a LimitParameter fixed it all up, but it took me a while to get there. 😆

MiroKentico commented 2 years ago

Hi @mattnield, you are right, you run into issue that has same root cause as issues reported in the first comment (initial issues). That is wrongly handled error response from Delivery API.

The issue you run into will be solved in conceptually same way as initial issues, that means you will get more useful error message in fixed version of SDK :-)

MiroKentico commented 2 years ago

Just FYI, this was released in https://github.com/Kentico/kontent-delivery-sdk-net/releases/tag/16.0.1.