Laravel | Queue failed : Missing argument 1 for App\Jobs\SendCollectionSMS: - php

All my jobs fail because of this strange error and can't figure out whats the problem here.
Job:
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public $fName, $lName, $colAmt, $colDate, $totalAmt, $balAmt, $dueDate, $mobile_no, $is_loan_complete;
public function __construct($fName, $lName, $colAmt, $colDate, $totalAmt, $balAmt, $dueDate, $mobile_no, $is_loan_complete)
{
//
$this->fName = $fName;
$this->lName = $lName;
$this->colAmt = $colAmt;
$this->colDate = $colDate;
$this->totalAmt = $totalAmt;
$this->balAmt = $balAmt;
$this->dueDate = $dueDate;
$this->mobile_no = $mobile_no;
$this->is_loan_complete = $is_loan_complete;
}
/**
* Execute the job.
*
* #return void
*/
public function handle($fName, $lName, $colAmt, $colDate, $totalAmt, $balAmt, $dueDate, $mobile_no, $is_loan_complete)
{
//
$username = "xxxxxxxxxx";
$hash = "xxxxxxxxxxxxxxxxxxxxxxxxx";
// Config variables. Consult http://api.textlocal.in/docs for more info.
$test = "0";
// Data for text message. This is the text message data.
$sender = "xxxxx"; // This is who the message appears to be from.
$numbers = $mobile_no; // A single number or a comma-seperated list of numbers
$message = "Dear ".$fName." ".$lName.", your payment of Rs. ".$colAmt." has been received on ".$colDate.".
Total: Rs. ".$totalAmt.", Balance: ".$balAmt.", Due Date: ".$dueDate.". Thank You";
// 612 chars or less
// A single number or a comma-seperated list of numbers
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('http://api.textlocal.in/send/?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $result = curl_exec($ch); // This is the result from the API
curl_close($ch);
$this->pusher($result);
}
From the error in my failed_jobs table I notice that the handle function doesn't even run due the argument error
Exception thrown:
exception 'ErrorException' with message 'Missing argument 1 for App\Jobs\SendCollectionSMS::handle()' in C:\xampp\htdocs\financetest1\app\Jobs\SendCollectionSMS.php:42
Stack trace:
#0 C:\xampp\htdocs\financetest1\app\Jobs\SendCollectionSMS.php(42): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Missing argumen...', 'C:\\xampp\\htdocs...', 42, Array)
#1 [internal function]: App\Jobs\SendCollectionSMS->handle()
#2 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(30): call_user_func_array(Array, Array)
#3 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#4 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#5 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Container\Container.php(531): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#6 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#7 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(114): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(App\Jobs\SendCollectionSMS))
#8 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(App\Jobs\SendCollectionSMS))
#9 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#10 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(43): Illuminate\Bus\Dispatcher->dispatchNow(Object(App\Jobs\SendCollectionSMS), NULL)
#11 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(69): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#12 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(291): Illuminate\Queue\Jobs\Job->fire()
#13 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(258): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#14 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(110): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#15 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(101): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))
#16 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(85): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
#17 [internal function]: Illuminate\Queue\Console\WorkCommand->fire()
#18 C:\xampp\htdocs\financetest1\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(30): call_user_func_array(Array, Array)
I'm calling the job from a controller like :(sorry for the mess in below code but it works)
dispatch(new SendCollectionSMS(ucwords($collection->loan->customer->first_name), ucwords($collection->loan->customer->last_name), $collection->collected_amount, Carbon::parse($collection->created_at)->format('d-m-Y'), $collection->loan->loan_amount, ($collection->loan->loan_amount - $collection->loan->collected_amount), Carbon::parse($collection->loan->end_date)->format('d-m-Y'), $collection->loan->customer->mobile_no, false));
Now I am positive that the arguments which I am passing when creating a new job in my controller are proper but It seems there is an issue in the way I'm referencing the variables in my handler.
Whats going wrong here?

This is a very old post, but for anyone who runs into this problem.
Your handle function doesn't need any variables
public function handle()
Within this function you can get your variables by calling them as followed:
$this->fName
If you still run into the "missing argument 1" problem: Restart your artisan command
php artisan queue:work
This took me a while to figure out, changing your Job file has no effect if your queue:work is already running.

Related

Why i am getting Fatal error: require(): Failed opening required init_autoloader.php with zend

I am using ubuntu 15.10 and i have install zend 3 I am new to zend framework so i have started it with its Skeleton Application tutorial after setting everything from zend official tutorial i am getting this error :-
Warning: require(init_autoloader.php): failed to open stream: No such file or directory in /var/www/html/zendapp/public/index.php on line 25
Fatal error: require(): Failed opening required 'init_autoloader.php' (include_path='.:/usr/share/php') in /var/www/html/zendapp/public/index.php on line 25
My index.php code:-
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* Display all errors when APPLICATION_ENV is development.
*/
if ($_SERVER['APPLICATION_ENV'] == 'development') {
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
return false;
}
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
Please help me in resolving this issue or tell me if any php modules i need to install thanks.
Update :-
After lots of research my add,delete functions and view files are working but when i click on edit link of Albums I am getting an error :-
Zend\Stdlib\Exception\BadMethodCallException
File:
/var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Stdlib/Hydrator/ArraySerializable.php:29
Message:
Zend\Stdlib\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy()
Stack trace:
#0 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Form/Fieldset.php(590): Zend\Stdlib\Hydrator\ArraySerializable->extract(Object(Album\Model\Album))
#1 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Form/Form.php(881): Zend\Form\Fieldset->extract()
#2 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Form/Form.php(303): Zend\Form\Form->extract()
#3 /var/www/html/zendapp/module/Album/src/Album/Controller/AlbumController.php(75): Zend\Form\Form->bind(Object(Album\Model\Album))
#4 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Album\Controller\AlbumController->editAction()
#5 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#6 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#7 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#8 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#10 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#11 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#12 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 /var/www/html/zendapp/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 /var/www/html/zendapp/public/index.php(26): Zend\Mvc\Application->run()
#15 {main}
Code Files
edit Action :-
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('album', array(
'action' => 'add'
));
}
// Get the Album with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$album = $this->getAlbumTable()->getAlbum($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('album', array(
'action' => 'index'
));
}
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getAlbumTable()->saveAlbum($album);
// Redirect to list of albums
return $this->redirect()->toRoute('album');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
2.)
edit.phtml
<?php
// module/Album/view/album/album/edit.phtml:
$title = 'Edit album';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url(
'album',
array(
'action' => 'edit',
'id' => $this->id,
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();

Monolog get requested url and simple log on in Laravel 5

I have checked the official repo of monolog and put together below code, its working great and I am getting response in Slack. But the response are very verbose, How Can i get simple response, without Stack Trace.
And as suggested on doc I have added $monolog->pushProcessor(new WebProcessor($_SERVER)); but its not giving any information about requested URL and server.
class AppServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
if ($this->app->environment('production')) {
// Get The Logger
$monolog = Log::getMonolog();
// **********************************************************************************************
// I have tried official WebProcessor to get url, but its not giving me anything
// **********************************************************************************************
$monolog->pushProcessor(new WebProcessor($_SERVER));
$monolog->pushProcessor(function ($record) {
$record['extra']['session_id'] = Cookie::get(config('session.cookie'));
$record['extra']['request_id'] = Session::get('request_id');
return $record;
});
$slackHandler = new SlackHandler(env('SLACK_TOKEN'), '#sss-sslogs', 'sss-log', true, ':warning:', Logger::ERROR);
// **********************************************************************************************
// Setup Line Formatter but no luck
// **********************************************************************************************
// the default date format is "Y-m-d H:i:s"
$dateFormat = "Y n j, g:i a";
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
$output = "%datetime% > %level_name% > %message% %context% %extra%\n";
// finally, create a formatter
$formatter = new LineFormatter($output, $dateFormat);
$slackHandler->setFormatter($formatter);
$monolog->pushHandler($slackHandler);
}
}
Above code is giving me below response in Slack channel
exception 'Illuminate\Database\Eloquent\ModelNotFoundException' with message 'No query results for model [App\User].' in /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:151
Stack trace:
#0 /home/vagrant/Code/App/app/Http/Controllers/PortfolioController.php(30): Illuminate\Database\Eloquent\Builder->firstOrFail()
#1 [internal function]: App\Http\Controllers\PorofileController->show('users')
#2 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(246): call_user_func_array(Array, Array)
#3 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(162): Illuminate\Routing\Controller->callAction('show', Array)
#4 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(107): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\PortfolioController), Object(Illuminate\Routing\Route), 'show')
#5 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#6 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#8 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#9 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(108): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#10 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(67): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\PortfolioController), Object(I…
Level
----------------
ERROR
Where is the requested URL and session data??
How can I get only below part with requested URL, I am not interested in Stack trace:
exception 'Illuminate\Database\Eloquent\ModelNotFoundException' with message 'No query results for model [App\User].' in /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:151

How to fix GuzzleHttp\Exception\ServerException when using a Pool?

The docs say I can create a pool and any failed requests should call the "rejected" method, but instead I'm getting GuzzleHttp\Exception\ServerException. Here's my code:
$client = new GuzzleHttp\Client([
'base_uri' => ServerConfig::Json('file_server'),
]);
$requests = function() use ($client, $delete_time) {
foreach($this->pcs_master->pcs as $id => $pcs) {
$paths = $pcs->database->SelectSimpleArray('wopi_doc', 'wd_filepath', ['wd_deleted_at IS NOT NULL', 'wd_deleted_at < ?' => $delete_time]);
if ($paths) {
foreach (array_chunk($paths, self::WOPI_SOFT_DELETE_CHUNK_SIZE) as $chunk) {
yield $client->delete('/', [
'body' => json_encode(['paths' => $files]),
]);
}
}
}
};
$pool = new GuzzleHttp\Pool($client, $requests(), [
'concurrency' => self::WOPI_SOFT_DELETE_MAX_CONCURRENT_REQUESTS,
'fulfilled' => function ($response, $index) {
dump('fulfilled', $response, $index);
},
'rejected' => function ($reason, $index) {
dump('rejected', $reason, $index);
},
]);
$pool->promise()->wait();
Here's the exception:
TYPE: GuzzleHttp\Exception\ServerException
MESSAGE: Server error: 500
FILE: /path/to/my/project/vendor/guzzlehttp/guzzle/src/Middleware.php(68)
=== TRACE ===
#0 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(199): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#1 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(152): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#2 /path/to/my/project/vendor/guzzlehttp/promises/src/TaskQueue.php(60): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#3 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(240): GuzzleHttp\Promise\TaskQueue->run(true)
#4 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(217): GuzzleHttp\Promise\Promise->invokeWaitFn()
#5 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(261): GuzzleHttp\Promise\Promise->waitIfPending()
#6 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(219): GuzzleHttp\Promise\Promise->invokeWaitList()
#7 /path/to/my/project/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending()
#8 /path/to/my/project/vendor/guzzlehttp/guzzle/src/Client.php(129): GuzzleHttp\Promise\Promise->wait()
#9 /path/to/my/project/vendor/guzzlehttp/guzzle/src/Client.php(87): GuzzleHttp\Client->request('delete', '/', Array)
#10 /path/to/my/project/class/crondaemon.php(5057): GuzzleHttp\Client->__call('delete', Array)
#11 /path/to/my/project/class/crondaemon.php(5057): GuzzleHttp\Client->delete('/', Array)
#12 /path/to/my/project/vendor/guzzlehttp/guzzle/src/Pool.php(55): CronDaemon::{closure}()
#13 [internal function]: GuzzleHttp\Pool::GuzzleHttp\{closure}()
#14 /path/to/my/project/vendor/guzzlehttp/promises/src/EachPromise.php(73): Generator->rewind()
#15 /path/to/my/project/vendor/guzzlehttp/guzzle/src/Pool.php(74): GuzzleHttp\Promise\EachPromise->promise()
#16 /path/to/my/project/class/crondaemon.php(5073): GuzzleHttp\Pool->promise()
#17 [internal function]: CronDaemon->DeleteWopiDocs()
#18 /path/to/my/project/tests/cron.php(46): call_user_func(Array)
#19 /path/to/my/project/tests/cron.php(51): CronDaemonTestScript::main(Array)
#20 {main}
It looks like the exception is being thrown on the $client->delete line, but I thought that was supposed to create a Request object but not send it yet (that's the job of the pool)?
client::get(), client::put(), client::post(), client::delete() are abstractions for client::request() which in itself is an abstraction for client::requestAsync().You are attempting to yield a ResponseInterface instance.
If you are going to use GuzzleHttp\Pool, you will have to manually create the Psr\http-message\Request objects.
yield new GuzzleHttp\Psr7\Request($method, $uri, $headers_array, $body, $protocol_version);
Further information can be obtained by checking out concurrent requests and GuzzleHttp\Psr7\Request
Edit: Response to question in comments.
Ultimately, each request you want to have sent, is sent using Client::sendAsync(). This means that any option previously configured within the Client will remain valid while it is being used within the pool.
It appears that I have to wrap the Promise in a function for some reason:
foreach (array_chunk($paths, self::WOPI_SOFT_DELETE_CHUNK_SIZE) as $chunk) {
yield function() use ($client, $files) {
return $client->deleteAsync('/', [
'body' => json_encode(['paths' => $files]),
]);
};
}

Laravel Dingo Api - How to return JSON formatted error response from API Controller?

In my routes.php, I have this:
$apiSettings = [
'version' => 'v1',
'prefix' => 'api',
'protected' => true
];
Route::api($apiSettings, function() {
Route::get('venue', 'ApiDataController#venue');
});
The protected venue API route accesses a controller method. The controller method performs the Eloquent Query on a Venues model, and returns the response. This works perfectly.
The issue is in if I want to return an error - I am unsure how to. Here is the Venue Method:
public function venue(){
try {
//Some code that returns an exception
} catch(someexception $e) {
//How do I return the exception such that Dingo will parse it into a proper JSON response?
}
$response = Venue::with('address')->get();
return $response;
}
My attempted solution (in the try block):
try {
//some code that returns an exception
} catch(someexception $e) {
$response = array(
'message' => 'some random exception message'
);
return Response::json($response, 403);
}
I got the following error when I attempted to do that:
Argument 1 passed to Dingo\Api\Http\Response::makeFromExisting() must be an instance of Illuminate\Http\Response, instance of Illuminate\Http\JsonResponse given, called in /vagrant/www/planat-app/vendor/dingo/api/src/Routing/Router.php on line 165 and defined
Second Attempted Solution:
From Dingo's Returning Errors, docs, I tested what would happen if I returned one of the exceptions:
public function venue(){
throw new Symfony\Component\HttpKernel\Exception\ConflictHttpException('err);
}
However, instead of returning the error as a JSON response, a laravel error page comes up, with the following error displayed:
[internal function]: ApiDataController->venue()
#1 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231):
call_user_func_array(Array, Array)
#2 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(93):
Illuminate\Routing\Controller->callAction('venue', Array)
#3 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(62):
Illuminate\Routing\ControllerDispatcher->call(Object(ApiDataController),
Object(Illuminate\Routing\Route), 'venue')
#4 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(930):
Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route),
Object(Dingo\Api\Http\InternalRequest), 'ApiDataControll...', 'venue')
#5 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#6 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(105):
call_user_func_array(Object(Closure), Array)
#7 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(996):
Illuminate\Routing\Route->run(Object(Dingo\Api\Http\InternalRequest))
#8 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(964):
Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\InternalRequest))
#9 /vagrant/www/planat-app/vendor/dingo/api/src/Routing/Router.php(147):
Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\InternalRequest))
#10 /vagrant/www/planat-app/vendor/dingo/api/src/Dispatcher.php(337): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\InternalRequest))
#11 /vagrant/www/planat-app/vendor/dingo/api/src/Dispatcher.php(278): Dingo\Api\Dispatcher->dispatch(Object(Dingo\Api\Http\InternalRequest))
#12 /vagrant/www/planat-app/vendor/dingo/api/src/Dispatcher.php(213): Dingo\Api\Dispatcher->queueRequest('get', 'venue', Array)
#13 /vagrant/www/planat-app/app/routes.php(51): Dingo\Api\Dispatcher->get('venue')
#14 [internal function]: {closure}()
#15 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(105):
call_user_func_array(Object(Closure), Array)
#16 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(996):
Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#17 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(964):
Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#18 /vagrant/www/planat-app/vendor/dingo/api/src/Routing/Router.php(147):
Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#19 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(738):
Dingo\Api\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#20 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(708):
Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#21 /vagrant/www/planat-app/vendor/dingo/api/src/Http/Middleware/RateLimit.php(97):
Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request),
1, true)
#22 /vagrant/www/planat-app/vendor/dingo/api/src/Http/Middleware/Authentication.php(102):
Dingo\Api\Http\Middleware\RateLimit->handle(Object(Illuminate\Http\Request),
1, true)
#23 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Session/Middleware.php(72):
Dingo\Api\Http\Middleware\Authentication->handle(Object(Illuminate\Http\Request),
1, true)
#24 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php(47):
Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request),
1, true)
#25 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php(51):
Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1,
true)
#26 /vagrant/www/planat-app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23):
Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1,
true)
#27 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(606):
Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#28 /vagrant/www/planat-app/public/index.php(49): Illuminate\Foundation\Application->run()
As you are building api, you can catch similar types of exception globally. For example, if a user tried to get a customer with an ID which doesn't exists, then you could do this.
Customer::findOrFail($id);
then you could catch all of this type exception in app/start/global.php like this.
App::error(function(ModelNotFoundException $modelNotFoundException){
$errorResponse = [
'errors' => 'Not found any resource',
'message' => $modelNotFoundException->getMessage()
];
return Response::json($errorResponse, 404); //404 = Not found
});
Reading from Dingo's Returning Errors docs, it says:
Instead of manually creating and returning an error response you can simply throw an exception and the package will handle the exception and return an appropriate response.
The following is a list of all the supported exceptions that you should throw when you encounter an error.
Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
Symfony\Component\HttpKernel\Exception\BadRequestHttpException
Symfony\Component\HttpKernel\Exception\ConflictHttpException
Symfony\Component\HttpKernel\Exception\GoneHttpException
Symfony\Component\HttpKernel\Exception\HttpException
Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException
Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException
Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException
Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException
It also supports some generic resource exceptions that you can pass validation errors onto as well:
Dingo\Api\Exception\DeleteResourceFailedException
Dingo\Api\Exception\ResourceException
Dingo\Api\Exception\StoreResourceFailedException
Dingo\Api\Exception\UpdateResourceFailedException
So in short, you need to throw one of the exceptions above that Dingo supports back to Dingo. For example:
try {
//Some code that returns an exception
} catch(SomeException $e) {
throw new Symfony\Component\HttpKernel\Exception\HttpException($e->getMessage);
}
Or in fact, if the exception thrown is one of the type above, or one that extends them, you can just remove your try/catch clause completely. The exception should be automatically thrown back to Dingo to handle it.
Please check this:
try {
//some code that returns an exception
} catch(\Exception $e) {
$response = array(
'message' => 'some random exception message'
);
return response()->json($response, 403);
}
Please check and let me know.

Exception raised in Varien_File_Uploader when using XMLRPC to modify categories

I'm using the Magento (1.6.2.0) XMLRPC-API to assign/remove products from a category. I recently looked into the exception logs and noticed that there are a lot of exceptions. Whenever I update the category the following error occurs:
2012-03-14T10:35:33+00:00 ERR (3):
exception 'Exception' with message '$_FILES array is empty' in /path/to/magento/includes/src/Varien_File_Uploader.php:461
Stack trace:
#0 /path/to/magento/includes/src/Varien_File_Uploader.php(149): Varien_File_Uploader->_setUploadFileId('image')
#1 /path/to/magento/includes/src/Mage_Catalog_Model_Category_Attribute_Backend_Image.php(57): Varien_File_Uploader->__construct('image')
#2 [internal function]: Mage_Catalog_Model_Category_Attribute_Backend_Image->afterSave(Object(Mage_Catalog_Model_Category))
#3 /path/to/magento/includes/src/__default.php(39967): call_user_func_array(Array, Array)
#4 /path/to/magento/includes/src/__default.php(40958): Mage_Eav_Model_Entity_Abstract->walkAttributes('backend/afterSa...', Array)
#5 /path/to/magento/includes/src/Mage_Catalog_Model_Resource_Category.php(235): Mage_Eav_Model_Entity_Abstract->_afterSave(Object(Mage_Catalog_Model_Category))
#6 /path/to/magento/includes/src/__default.php(40434): Mage_Catalog_Model_Resource_Category->_afterSave(Object(Mage_Catalog_Model_Category))
#7 /path/to/magento/includes/src/__default.php(5593): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Category))
#8 /path/to/magento/includes/src/Mage_Catalog_Model_Category_Api.php(528): Mage_Core_Model_Abstract->save()
#9 [internal function]: Mage_Catalog_Model_Category_Api->removeProduct(23, '2743')
#10 /path/to/magento/includes/src/Mage_Api_Model_Server_Handler_Abstract.php(292): call_user_func_array(Array, Array)
#11 [internal function]: Mage_Api_Model_Server_Handler_Abstract->call('[removed]', 'category.remove...', Array)
#12 /path/to/magento/includes/src/Zend_Server_Abstract.php(232): call_user_func_array(Array, Array)
#13 /path/to/magento/includes/src/Zend_XmlRpc_Server.php(599): Zend_Server_Abstract->_dispatch(Object(Zend_Server_Method_Definition), Array)
#14 /path/to/magento/includes/src/Zend_XmlRpc_Server.php(337): Zend_XmlRpc_Server->_handle(Object(Zend_XmlRpc_Request_Http))
#15 /path/to/magento/includes/src/Mage_Api_Model_Server_Adapter_Xmlrpc.php(105): Zend_XmlRpc_Server->handle()
#16 /path/to/magento/includes/src/Mage_Api_Model_Server.php(84): Mage_Api_Model_Server_Adapter_Xmlrpc->run()
#17 /path/to/magento/app/code/core/Mage/Api/controllers/XmlrpcController.php(39): Mage_Api_Model_Server->run()
#18 /path/to/magento/includes/src/__default.php(13333): Mage_Api_XmlrpcController->indexAction()
#19 /path/to/magento/includes/src/__default.php(17589): Mage_Core_Controller_Varien_Action->dispatch('index')
#20 /path/to/magento/includes/src/__default.php(17180): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#21 /path/to/magento/includes/src/__default.php(19672): Mage_Core_Controller_Varien_Front->dispatch()
#22 /path/to/magento/app/Mage.php(640): Mage_Core_Model_App->run(Array)
#23 /path/to/magento/index.php(80): Mage::run('brillen', 'website')
#24 {main}
The category is updated successfully, but I'm still wondering why this happens.
A workaround:
Add this rewrite to a custom module (ie. catalogextended) in config.xml
<models>
<catalogextended>
<class>Myname_Catalogextended_Model</class>
</catalogextended>
<catalog>
<rewrite>
<category_attribute_backend_image>Myname_Catalogextended_Model_Category_Attribute_Backend_Image</category_attribute_backend_image>
</rewrite>
</catalog>
</models>
And add this file:
app/code/local/Myname/Catalogextended/Model/Category/Attribute/Backend/Image.php
Containing:
/**
* Save uploaded file and set its name to category
*
* #param Varien_Object $object
*/
public function afterSave($object)
{
$value = $object->getData($this->getAttribute()->getName());
if (is_array($value) && !empty($value['delete'])) {
$object->setData($this->getAttribute()->getName(), '');
$this->getAttribute()->getEntity()
->saveAttribute($object, $this->getAttribute()->getName());
return;
}
/* Workaround to avoid exception '$_FILES array is empty' when assiging
* products to a category or creating a category with the API.
* Inspired by http://www.magentocommerce.com/bug-tracking/issue/?issue=11597
*/
if (!isset($_FILES) || count($_FILES) == 0)
{
return;
}
$path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
try {
$uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($path);
$object->setData($this->getAttribute()->getName(), $result['file']);
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
} catch (Exception $e) {
if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) {
Mage::logException($e);
}
/** #TODO ??? */
return;
}
}
}
Other option inspired in Mondane Answer is:
copy /app/code/core/Mage/Catalog/Model/Category/Attribute/Backend/Image.php to /app/code/local/Mage/Catalog/Model/Category/Attribute/Backend/Image.php
(if you don't have the path CREATE IT)
after that edit the file you have created
replace afterSave function with this code
public function afterSave($object)
{
$value = $object->getData($this->getAttribute()->getName());
if (is_array($value) && !empty($value['delete'])) {
$object->setData($this->getAttribute()->getName(), '');
$this->getAttribute()->getEntity()
->saveAttribute($object, $this->getAttribute()->getName());
return;
}
/* Workaround to avoid exception '$_FILES array is empty' when assiging
* products to a category or creating a category with the API.
* Inspired by http://www.magentocommerce.com/bug-tracking/issue/?issue=11597
*/
if (!isset($_FILES) || count($_FILES) == 0)
{
return;
}
$path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
try {
$uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($path);
$object->setData($this->getAttribute()->getName(), $result['file']);
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
} catch (Exception $e) {
if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) {
Mage::logException($e);
}
/** #TODO ??? */
return;
}
}
}
by doing this, you'll replace magento code without getting involved in core files, beacause magento will take this file automatically before taking the original file

Categories