How can I fail wp_action_scheduler cron job without site break? - wordpress

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.

Related

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;
}

FOSUserBundle unlock user on login: changes are not persisted

I have locked users that can re-activate their account if they enter a valid ValidationCode on login. As the user is previously locked, I use AuthenticationFailureListener to detect the login failure and determine if there is a valid validation code, according to log tracing every action seems to have correct data and everything should work ok but the problem is the data remains the same, nothing is persisted in database, so no change occurs.
This is my code:
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$userReactivated = false;
// Checks if error is due to user locked and
if ( $exception instanceof LockedException )
{
$username = $request->request->get("_username");
$v_code = $request->request->get("_validation_code");
error_log("{$username} {$v_code}");
$user = $this->em->getRepository("AppUserBundle:Usuario")->findOneBy(array('username' => $username));
if ($user != null)
{
$validationCode = $this->em->getRepository("AppUserBundle:ValidationCode")->findOneBy(array('code' => $v_code));
if ($validationCode != null)
{
error_log("Code " . $validationCode->getId());
if ($validationCode->isActive())
{
$user->setValidationCode($validationCode);
$userReactivated = true;
$user->setLocked(false);
$this->em->persist($user);
$this->em->flush();
}
}
}
}
if (!$userReactivated)
return parent::onAuthenticationFailure($request, $exception);
else
{
return true; // TODO: login user and redirect to home page
}
}
if (!$userReactivated) {
$exception->setUser($user);
return parent::onAuthenticationFailure($request, $exception);
} else {
return true; // TODO: login user and redirect to home page
}

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?

Google_Service_Exception error 500 not caught

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

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