Google_Service_Exception error 500 not caught - symfony

I am using the google api client to get a list of blogger posts (see below code).
try {
// create service and get data
$blogger = new \Google_Service_Blogger($this->client);
// https://developers.google.com/blogger/docs/3.0/reference/posts/list
// GET https://www.googleapis.com/blogger/v3/blogs/blogId/posts
if ($this->cache->contains('posts')) {
$posts = $this->cache->fetch('posts');
}
else {
$posts = $blogger->posts->listPosts($this->bloggerId, $optParams);
$this->cache->save('posts', $posts, 600); // 600 = 10 mins
}
return $posts;
} catch (apiServiceException $e) {
// Error from the API.
//print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
This works fine on most occasions, but occasionally I get an API error which is not caught.
Google_Service_Exception: Error calling GET https://www.googleapis.com/blogger/v3/blogs/8894809254783984374/posts?maxResults=5: (500) Backend Error at /var/www/releases/20140607051057/vendor/google/apiclient/src/Google/Http/REST.php:79)"} []
Does anyone know why this error is thrown, and how I can deal with it?
Cheers

The problem is with the namespaces. You can use the generic:
} catch (\Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
or, more specific:
try {
...
} catch (\Google_Service_Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
Exception is the base class for all Exceptions. See http://php.net/manual/en/class.exception.php

Related

How can I fail wp_action_scheduler cron job without site break?

Wp action-scheduler job is running successfully but I want it to fail when any exception comes in the code.
try {
$records = some records in array;
foreach ($records as $r) {
$sku = $r["SKU"] ?? null;
if (!isset($sku)) {
throw new \Exception("SKU is missing");
return;
}
$product_id = Wp::wc_get_product_id_by_sku($sku);
Wp::update_post_meta($product_id, "_regular_price", 500);
}
} catch (\Exception $e) {
echo($e);
**//Here I want to fail the action scheduler job**
return false;
}
In my case, everything works correctly but it should be failed schedule job if comes into catch (\Exception $e){---} without site break.

Id of the connection in Ratchet web socket requests

I am testing Ratchet with Symfony. Tests are fine but I cannot understand where a property $conn->resourceID is coming from... Here is the code from the official tutorial
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
I cannot find it ($conn->resourceID), IDE cannot find it, but it works, it is populated somehow....
Does anyone has a clue to help me to understand?
Thanks

404 cehck status for current page

i want to check current page's 404 status in a plugin file
i tried this but page only spinning and never finish loading
public static function check_404_status($my_url)
{
$result = false;
$my_url = self::tc_trim_slash($my_url);
$ch = curl_init($my_url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($retcode != 200) {
$result=true;
return $result;
}
else {
// echo "Specified URL exists";
}
curl_close($ch);
}
i have also tried this but it gives error
is_404 was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.)
public static function check_404_status($my_url)
{
$result = false;
$my_url = self::tc_trim_slash($my_url);
if(is_404())
{
$result=true;
return $result;
}
}
I am new to wordpress, can someone please help me to solve this??
is_404() conditional is set in the WP_Query class and checks is the query returns a 404 (returns no results)
So, you can try to modify your function like that
public static function check_404_status()
{
global $wp_query;
if (isset($wp_query)) {
return $wp_query->is_404();
}
return false;
}

New Symfony Project Error

I just created a new project with Symfony (This is not my first time), but when I run the application in the browser, appears this error:
Parse error: syntax error, unexpected '{' in
/var/www/ProjectName/var/bootstrap.php.cache on line 2094
If I go to that line in that file there is :
$this->loading[$id] = true;
try {
$service = $this->$method();
} catch (\Exception $e) {
unset($this->services[$id]);
throw $e;
} finally {
unset($this->loading[$id]);
}
return $service;
}
The line 2094 is this piece:
} finally {
What I have to do to make it work?

Returning status code in laravel

I am a total noob at Laravel
public function stageFiles($user, $project) {
try
{
$path = Input.get("item");
}
catch (Exception $e)
{
$exceptionMessage = $e->getMessage();
$responseText = json_encode($exceptionMessage);
}
}
What I want to do is return the response text and a status code, 500, how can I do this?
This is how you send a 500:
public function stageFiles($user, $project) {
try
{
$path = Input.get("item");
}
catch (Exception $e)
{
$exceptionMessage = $e->getMessage();
$responseText = json_encode($exceptionMessage);
return Response::make($responseText, 500);
}
}

Resources