JustGiving / JustGivingNETSDK

JustGiving C# SDK
1 stars 1 forks source link

Unauthorized error is not surfaced, instead get a different exception that hides the cause #3

Open Blackbaud-ChristiSchneider opened 6 years ago

Blackbaud-ChristiSchneider commented 6 years ago

I was trying to query fundraising pages and I started getting an error that showed as Cannot access a disposed object. After checking the JustGiving client logs, I noticed an Unauthorized error in the logs. Reauthenticating with OAuth and getting a fresh access token fixed the error.

I think the correct error is being thrown but in a catch or finally block, another error is being caused by the HttpClient. This is very confusing for developers when it's really masking a simple issue like an expired token.

Code:

                var client = new JustGivingApiClient(ClientId, ClientSecret, new OAuthAccessToken(accessToken));
                client.UseProduction();
                client.LogEverything();
                var logger = new TestLogProvider();
                client.LogProvider = logger;

                return await client.Fundraising.GetFundraisingPages();

Error:

Cannot access a disposed object.
Object name: 'System.Net.Http.StreamContent'.
   at System.Net.Http.HttpContent.CheckDisposed()
   at System.Net.Http.HttpContent.ReadAsStringAsync()
   at JustGivingSDK.Clients.ClientBase.<Execute>c__async2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at JustGivingSDK.Clients.Fundraising.FundraisingClient.<GetFundraisingPages>c__async10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at MyService.BusinessLogic.JustGivingService.<GetPages>d__8.MoveNext() in C:\code\MyService\BusinessLogic\JustGivingService.cs:line 149

Logs from JustGiving client:

[ERROR] An error occurred while executing an HTTP request
[WARN ] 
[HttpRequest] GET https://api.justgiving.com/v1/fundraising/pages
[AuthorizationMode] OAuth
[AppId] [redacted]
[ApplicationKey?] Yes

[HttpHeader] Accept: application/json

[HttpHeader] Authorization: Bearer [redacted]

[HttpHeader] x-application-key: [redacted]

[HttpHeader] x-app-id: [redacted]

[HttpHeader] x-justgiving-sdk-version: 1.0.0.0

[HttpResponse]
[HttpHeader] Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, Referer, User-Agent, Origin, Accept, Host, x-app-id, x-api-key, x-application-key

[HttpHeader] Access-Control-Allow-Methods: HEAD, GET, POST, PUT, DELETE, PATCH, OPTIONS

[HttpHeader] Access-Control-Allow-Origin: *

[HttpHeader] Access-Control-Max-Age: 300

[HttpHeader] Age: 0

[HttpHeader] JG-proxy-layer: app

[HttpHeader] JG-proxy-layer: waf

[HttpHeader] X-Country-Code: US

[HttpHeader] X-Frame-Options: SAMEORIGIN

[HttpHeader] X-JustGiving-MachineName: [redacted]

[HttpHeader] X-JustGiving-Operation: FundraisingApi:GetFundraisingPages

[HttpHeader] X-JustGiving-RenderTime: 123ms

[HttpHeader] X-JustGiving-UTCTimeStamp: 02/14/2018 00:53:16

[HttpHeader] X-JustGiving-Version: 1.1.600

[HttpHeader] X-XSS-Protection: 1

[HttpHeader] Connection: keep-alive

[HttpHeader] Cache-Control: private

[HttpHeader] Date: Wed, 14 Feb 2018 00:53:23 GMT

[HttpHeader] Server: Giving-Server

[HttpHeader] Server: 1.1

[HttpStatus] 401 Unauthorized