pnp / pnpcore

The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working with SharePoint Online and Teams which is agnostic to the underlying API's being called
https://aka.ms/pnp/coresdk/docs
MIT License
288 stars 188 forks source link

CSOM Errors reported as HTTP Response 200? #1413

Closed Gareth064 closed 4 months ago

Gareth064 commented 4 months ago

Category

Describe the bug

I was just looking through some exception logs in my application and I notice that when a CSOM request fails, the message it returns says HttpResponseCode: 200. Here is an example.

HttpResponseCode: 200 <<< This 
Message: Invalid date/time value.

A date/time field contains invalid data. Please check the value and try again.
ClientRequestId: b9bb10a1-40b3-8000-45b2-66430fcf07a5
ErrorTypeName: "Microsoft.SharePoint.SPException"

PnP.Core.CsomServiceException: CSOM service exception
   at PnP.Core.Services.BatchClient.ProcessCsomBatchResponse(CsomBatch csomBatch, String batchResponse, HttpStatusCode statusCode)
   at PnP.Core.Services.BatchClient.ExecuteCsomBatchAsync(Batch batch)
   at PnP.Core.Services.BatchClient.ExecuteBatch(Batch batch)
   at PnP.Core.Model.BaseDataModel`1.RequestAsync(ApiCall apiCall, HttpMethod method, String operationName)
   at PnP.Core.Model.BaseDataModel`1.RawRequestAsync(ApiCall apiCall, HttpMethod method, String operationName)
   at PnP.Core.Model.SharePoint.ListItem.UpdateOverwriteVersionAsync()
Error String: HttpResponseCode: 200 <<< This 
Message: Invalid date/time value.

A date/time field contains invalid data. Please check the value and try again.
ClientRequestId: b9bb10a1-40b3-8000-45b2-66430fcf07a5
ErrorTypeName: "Microsoft.SharePoint.SPException"

Error Message: Invalid date/time value.

A date/time field contains invalid data. Please check the value and try again. CSOM service exception 

Is this normal? Is it the PnP Core Library setting that code or is it direct from SPO?

Feels like it shouldn't be a 200 if the request failed.

Environment details (development & target environment)

DELETE THIS LINE BEFORE SUBMITTING - Complete the following (if ignored you'll be prompted for it before we can address your issue... save the time and provide it in your initial submission).

Additional context

I am trying to bubble the exception up to the client that is calling my Web Service, but I want to tailor my response code to either 400 or 500. So I was hoping to use the Response Code returned from SPO/PnP to inform my response.

jansenbe commented 4 months ago

@Gareth064 : When you do a batch request the HTTP response code typically is 200, but when the insides of the batch are inspected and there's something wrong we'll throw an error. I checked the code, and the HTTPStatus code you're getting is what SharePoint gave back to us. Relying on just the status code is not a good pattern here.

Gareth064 commented 4 months ago

Yeah if it's always 200 then I definitely can't rely on the pattern 🤣

I will just have to build a middleware parser as they are quick and dirty, until I come up with something else.