5am-code / laravel-notion-api

Effortless Notion integrations with Laravel
https://notionforlaravel.com
MIT License
410 stars 50 forks source link

NotionException's fromResponse() method throws an exception when $responseBody is null #132

Closed farez closed 1 year ago

farez commented 1 year ago

Describe the bug

In NotionException::fromResponse(), array_key_exists throws an exception when $responseBody is null, in the statement if (array_key_exists("code", $responseBody)).

To Reproduce

This cropped up on our production site but not sure how to reproduce it. It happened during a call to Notion::pages()->update($page);, and when the call fails inside Endpoint::patch() and an attempt is made to throw a NotionException.

Laravel 9. PHP 8.

Expected behavior

$responseBody is check to make sure it isn't null before calling array_keys with it.

Exceptions

[2023-04-20 09:40:45] production.ERROR: array_key_exists(): Argument #2 ($array) must be of type array, null given {"userId":1820,"exception":"[object] (TypeError(code: 0): array_key_exists(): Argument #2 ($array) must be of type array, null given at /home/forge/notioninvoice.com/vendor/fiveam-code/laravel-notion-api/src/Exceptions/NotionException.php:39)
[stacktrace]
#0 /home/forge/notioninvoice.com/vendor/fiveam-code/laravel-notion-api/src/Exceptions/NotionException.php(39): array_key_exists()
#1 /home/forge/notioninvoice.com/vendor/fiveam-code/laravel-notion-api/src/Endpoints/Endpoint.php(131): FiveamCode\\LaravelNotionApi\\Exceptions\\NotionException::fromResponse()
#2 /home/forge/notioninvoice.com/vendor/fiveam-code/laravel-notion-api/src/Endpoints/Pages.php(107): FiveamCode\\LaravelNotionApi\\Endpoints\\Endpoint->patch()
#3 /home/forge/notioninvoice.com/app/Http/Livewire/Invoice/NewInvoice.php(608): FiveamCode\\LaravelNotionApi\\Endpoints\\Pages->update()
#4 /home/forge/notioninvoice.com/app/Http/Livewire/Invoice/NewInvoice.php(479): App\\Http\\Livewire\\Invoice\\NewInvoice->updateInvoiceInNotion()
#5 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\\Http\\Livewire\\Invoice\\NewInvoice->createAndSend()
#6 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#7 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#8 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#9 /home/forge/notioninvoice.com/vendor/livewire/livewire/src/ComponentConcerns/HandlesActions.php(149): Illuminate\\Container\\BoundMethod::call()
#10 /home/forge/notioninvoice.com/vendor/livewire/livewire/src/HydrationMiddleware/PerformActionCalls.php(35): Livewire\\Component->callMethod()
#11 /home/forge/notioninvoice.com/vendor/livewire/livewire/src/LifecycleManager.php(82): Livewire\\HydrationMiddleware\\PerformActionCalls::hydrate()
#12 /home/forge/notioninvoice.com/vendor/livewire/livewire/src/Connection/ConnectionHandler.php(12): Livewire\\LifecycleManager->hydrate()
#13 /home/forge/notioninvoice.com/vendor/livewire/livewire/src/Controllers/HttpConnectionHandler.php(20): Livewire\\Connection\\ConnectionHandler->handle()
#14 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(48): Livewire\\Controllers\\HttpConnectionHandler->__invoke()
#15 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Route.php(261): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#16 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Route.php(204): Illuminate\\Routing\\Route->runController()
#17 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\\Routing\\Route->run()
#18 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#19 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#20 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#21 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#22 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#23 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#24 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#25 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Session/Middleware/AuthenticateSession.php(58): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#26 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Session\\Middleware\\AuthenticateSession->handle()
#27 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#28 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
#29 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Session\\Middleware\\StartSession->handle()
#30 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#31 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#32 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#33 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#34 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#35 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Router.php(697): Illuminate\\Pipeline\\Pipeline->then()
#36 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Router.php(672): Illuminate\\Routing\\Router->runRouteWithinStack()
#37 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\\Routing\\Router->runRoute()
#38 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute()
#39 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch()
#40 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#41 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#42 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#43 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(36): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#44 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#45 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#46 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#47 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#48 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#49 /home/forge/notioninvoice.com/vendor/fruitcake/laravel-cors/src/HandleCors.php(38): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#50 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle()
#51 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#52 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Http\\Middleware\\TrustProxies->handle()
#53 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#54 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then()
#55 /home/forge/notioninvoice.com/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#56 /home/forge/notioninvoice.com/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle()
#57 {main}
johguentner commented 1 year ago

Thank you @farez for sharing this! It is a rare occation, but it can happen.

✅ Fixed by #133

I'll release v1.0.1 in a minute, where this is fixed!

farez commented 1 year ago

Thanks for the quick fix!