From 94be255b535a0bef46fb6880e4f0dbb2e642b1f1 Mon Sep 17 00:00:00 2001 From: Charlie Bergthaler Date: Fri, 27 Apr 2018 17:39:58 +1200 Subject: [PATCH] Add context to logger warnings to prevent errors when using RaygunHandler for writing records. --- src/Extensions/SwiftypeSiteTree.php | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Extensions/SwiftypeSiteTree.php b/src/Extensions/SwiftypeSiteTree.php index 8ad007d..54b7e27 100644 --- a/src/Extensions/SwiftypeSiteTree.php +++ b/src/Extensions/SwiftypeSiteTree.php @@ -1,6 +1,8 @@ SwiftypeAPIKey; if (!$engineSlug) { + $trace = debug_backtrace(); $logger->warning( - 'Swiftype Engine Slug value has not been set. Settings > Swiftype Search > Swiftype Engine Slug' + 'Swiftype Engine Slug value has not been set. Settings > Swiftype Search > Swiftype Engine Slug', + array_shift($trace) //Add context (for RaygunHandler) by using the last item on the stack trace. ); return false; } if (!$domainID) { + $trace = debug_backtrace(); $logger->warning( - 'Swiftype Domain ID has not been set. Settings > Swiftype Search > Swiftype Domain ID' + 'Swiftype Domain ID has not been set. Settings > Swiftype Search > Swiftype Domain ID', + array_shift($trace) //Add context (for RaygunHandler) by using the last item on the stack trace. ); return false; } if (!$apiKey) { + $trace = debug_backtrace(); $logger->warning( - 'Swiftype API Key has not been set. Settings > Swiftype Search > Swiftype Production API Key' + 'Swiftype API Key has not been set. Settings > Swiftype Search > Swiftype Production API Key', + array_shift($trace) //Add context (for RaygunHandler) by using the last item on the stack trace. ); return false; @@ -105,14 +113,25 @@ protected function forceSwiftypeIndex() curl_close($ch); if (!$output) { + $trace = debug_backtrace(); $logger->warning( - 'We got no response from Swiftype for reindexing page: ' . $updateUrl + 'We got no response from Swiftype for reindexing page: ' . $updateUrl, + array_shift($trace) //Add context (for RaygunHandler) by using the last item on the stack trace. ); return false; } + $jsonOutput = json_decode($output, true); + if (!empty($jsonOutput) && array_key_exists('error', $jsonOutput)) { + $message = $jsonOutput['error']; + $context = ['exception' => new Exception($message)]; - return true; + //Add context (for RaygunHandler) by using the last item on the stack trace. + $logger->warning($message, $context); + return false; + } + + return false; } /**