In some cases and with YII_DEBUG=false, Yii is not using my error page - yii

My Yii error page is working perfectly, except in some cases. For example if I use
// Using "param" instead of params to cause exception
Yii::$app->para['test'];
With my config
'errorHandler' => [
'errorAction' => 'site/error',
],
Then I set YII_DEBUG to false
All I get is a white page with
An internal server error occurred.
Is this normal? With certain exceptions, Yii will just call die() or exit() with no other details?
Error code:
public function actionError()
{
Yii::$app->session->destroy();
if ($error = Yii::$app->errorHandler->exception) {
if (Yii::$app->request->isAjax)
echo $error->getMessage();
else
return $this->render('error', ['error' => $error]);
}
}

Related

Unexpected 404 Not Found Error in Laravel (5.8)

I have these lines in api.php
Route::apiResources([
'users' => 'Api\UserController',
'products' => 'Api\ProductController',
'categories' => 'Api\CategoryController',
]);
Route::get('/users/custom1', 'Api\UserController#custom1');
When I have them in this order and I call the route I get 404 Not Found.
But when I change the order, I get result:
Route::get('/users/custom1', 'Api\UserController#custom1');
Route::apiResources([
'users' => 'Api\UserController',
'products' => 'Api\ProductController',
'categories' => 'Api\CategoryController',
]);
This situation has frustrated me because I thought the error originated from the model and find method:
public function custom1()
{
$user2 = User::find(2);
return $user2;
}
Because I was checking the result in Postman and it shows the error like this when you ask for application/json as response:
{
"message": "No query results for model [App\\User] custom1",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"file": "C:\\wamp64\\www\\laravel-api\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php",
"line": 204,
"trace": [
{...
What is the reason of this change?
Because in both situations I can see the route in route:list.
Ok, I have realized the reason when I checked route:list with a clearer mind.
Laravel shows route:list in alphabetical order but actually the order we write in route file matters right?
So, it occured to me that if this line is coming first:
Laravel is trying to use custom1 as a route parameter.
So I must define this specific route before api resource routes:
Route::get('/users/custom1', 'Api\UserController#custom1');
Route::apiResources([
'users' => 'Api\UserController',
'products' => 'Api\ProductController',
'categories' => 'Api\CategoryController',
]);

How to get rid of error 422 laravel in a unit test?

So I'm writing unit tests for a laravel 5.7 web app and when I test the login it gives me error 422(I know that it has something to do with invalid data, I just don't know how to fix it)
public function testRegularUserLogin_CreatedRegularUse_ReturnsStoreView()
{
$regularUser = factory( User::class)->create();
$response = $this->json('POST','/login',
[
'email' => $regularUser->email,
'password' => $regularUser->password,
'_token' => csrf_token()
]);
$response->assertStatus(200);
}
I've tried using the csrf token on the header
This is the output that test gives me
You should just mock authentication:
do something like this
public function getFakeClient()
{
$client = factory(App\User::class)->create();
$this->be($client);
Auth::shouldReceive('user')->andReturn($this->client);
Auth::shouldReceive('check')->andReturn(true);
return $this->client;
}
then
$user = $this->getFakeClient();
$user->shouldReceive('posts')->once()->andReturn(array('posts'));
as recommended by Taylor Otwell himself here.

Magento Soap API 1.9 - failed to load external entity

when i tried to connect to Soap Api, i'm getting this error :
SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://www.lignedublanc.fr/index.php/api/soap/?wsdl' : failed to load external entity "https://www.lignedublanc.fr/index.php/api/soap/?wsdl
i tried with soap UI and same errors.
i tried everything on the net but nothing helped.
Maybe someone here can help me ?
<?php
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$url = 'https://www.lignedublanc.fr/index.php/api/soap/?wsdl=1';
$MagentoAPILogin = '';
$MagentoAPIPass = '';
$context = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
try {
echo "Connecting to : $url\n";
//$client = new SoapClient($url);
$client = new SoapClient($url, ['stream_context' => $context]);
echo $client->login($MagentoAPILogin, $MagentoAPIPass)."\n";
echo 'Login successful';
} catch (Exception $e) {
echo 'Login fail'."\n";
echo $e->getMessage()."\n";
echo $e->getTraceAsString()."\n";
}
I believe the problem is caused by the server not being able to access the API from the local machine. It is actually the .htaccess file that blocking any hosts except the development machines. Hence, it results in a 403 Forbidden error which is actually the SOAP error. For more reference, have a look:
Unable to connect to Magento SOAP API v2 due to "failed to load external entity"

Class 'KeenIO\Client\KeenIOClient' not found

I am including Keen in my product (code snippet below)
require INCLUDE_DIR . '/vendor/autoload.php'; // Autoloader for Composer (https://getcomposer.org/)
use KeenIO\Client\KeenIOClient;
class Statistics extends Model {
private $client;
public function __construct( $id = null ){
parent::__construct();
$this->client = KeenIOClient::factory([
'projectId' => KEEN_PROJECT_ID,
'writeKey' => KEEN_WRITE_KEY,
'readKey' => KEEN_READ_KEY
]);
}
...
but I continue to get an "Class 'KeenIO\Client\KeenIOClient' not found" error when the "KeenIOClient::factory" line runs. I was able to successfully install Keen.io through Composer - I feel it's something simple I'm missing - any ideas?
So I can't leave a comment, but I am wondering if there is maybe an issue with the include path? I was able to get this PHP snippet to work:
require 'vendor/autoload.php';
use KeenIO\Client\KeenIOClient;
$client = KeenIOClient::factory([
'projectId' => "53f3a8687d8cb95095000001",
'readKey' => "99a06e48fd7fb1279bc40995160eb0b61a9e0efaab8b651b029f0d895f77c0a804ba089282eff28bf8ad07f337422441d0542b7feaac9fea1e92fc153ee7efc51afad3276bda8d7754a338b349d540bfb402cba0dfdc82498c217054efd8abd0f47a0c0bc963bbdf0dc938c91b17d9f2"
]);
$count = $client->count('bitcoin-prices', [
'impressions' => [
'interval' => 'daily',
'timeframe' => 'this_30_days',
'group_by' => 'keen.timestamp'
]
]);
print_r($count);
That project id and read key are from the keen io open data sets (good to test with).

How hide PDOException with zend framework 2 on production environment?

We use a zend framework 2 for a web application.
We though to have disabled error_reporting and display_errors in our production environment.
But if an SQL error occured (It should not in production but ... :-) ), the exception is still displayed:
PDOException
File:
[...]/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:165
Message:
SQLSTATE[42000]: Syntax error or access violation
The query use Doctrine\DBAL\Statement (Doctrine2).
We cannot find where to globally catch this exception.
Ensure you have the correct view_manager config settings.
// config/autoload/global.php
return array(
'view_manager' => array(
'display_not_found_reason' => false,
'display_exceptions' => false,
),
);
Remember that this config is merged; if it's in your main global.php it will take preference over the module.config.php.
How it works
Every time ZF2 encounters and error (an exception) it will always catch the error. Instead of 'rethrowing' the exception information to the screen, the info is added to the MVC event and an 'error' event is triggered (either dispatch.error or render.error depending on where it is within the dispatch loop).
The Zend\Mvc\View\Http\ViewManager attaches 'error listeners' to handle these occurrences - (normally to show the error template). If you're using the standard skeleton application, the default error template will check the display_exceptions option and only render the error if it's enabled.
inside: Zend\Db\Adapter\Driver\Pdo\Connection
search for line :
$this->resource = new \PDO($dsn, $username, $password, $options);
$this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
and change it into:
$this->resource = new \PDO($dsn, $username, $password, $options);
$this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
I don't know if there is a way to override it globally without changing it in the library itself..
this is how i do it, inside index.php
try {
include ROOT . '/init_autoloader.php';
Zend\Mvc\Application::init(include 'config/application.config.php')->run();
} catch (Exception $e) {
if (IS_DEVELOPMENT_SERVER)
throw $e;
else {
echo('Error : ' . $e->getCode() . " " . $e->getMessage());
}
}