Yii2 Where to catch an exception thrown from a bootstrapper module? - yii

In my config, I have a custom class bootstrapping to fetch data
'bootstrap' => [
[
'class' => 'app\components\DataProvider',
'campaignJsonUrl' => 'https://example.com/api.json',
],
],
And I would like to catch exceptions throwns for an unreachable url or invalid json for example. But I can't figure out where to catch these.
I made a custom error handler:
'errorHandler' => [
// use 'site/error' action to display errors
'class' => 'app\components\ErrorHandler',
'errorAction' => 'site/error',
],
In which the error seems to reach ErrorHandler.renderException() method if I override it, however it never reaches the SiteController.actionError() method, I guess because it happens before any controller action. How can I make the ErrorHandler run SiteController.actionError() so it will show the website's error page with the correct layout?
NOTE: My overridden renderException() method only sends the exception to a Slack API then returns parent::renderException($exception)

Related

How to disable the "Global error handler detected" warning in vue test utils

I'm creating async tests using vue-test-utils and jest using the approach described here:
https://vue-test-utils.vuejs.org/guides/#what-about-nexttick
where you set Vue.config.errorHandler = done like shown below
test('Then we are shown events in EventCreate component', done => {
Vue.config.errorHandler = done
This is working and when an error is thrown in a promise handler in a component my tests will fail. However I'm getting this warning.
console.error node_modules/#vue/test-utils/dist/vue-test-utils.js:1421
[vue-test-utils]: Global error handler detected (Vue.config.errorHandler).
Vue Test Utils sets a custom error handler to throw errors thrown by instances. If you want this behavior in your tests, you must remove the global error handler.
I don't want to spam my test output with this warning. Is there a way to disable it?
This is how I did
beforeEach(() => {
jest.spyOn(console, 'error');
console.error.mockImplementation(() => 'some error');
});
afterEach(() => {
console.error.mockRestore();
});

Why does Laravel authentication require the 'web' guard?

I have setup Laravel 5.5 and installed the default authentication scaffolding.
My application has two types of users - customers and staff - so I prefer to name the authentication guards that way, and the following configuration seems to work.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customers' => [
'driver' => 'session',
'provider' => 'customer-users',
],
'staff' => [
'driver' => 'session',
'provider' => 'staff-users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
My providers, customer-users and staff-users, use the standard eloquent driver, however they each return a different user type.
The problem is that I would like to remove the 'web' guard, since it's just cluttering the config file. When I do however, I get an exception somewhere deep in the Laravel middleware.
I can live with the extra clutter of course, but it does bother me that Laravel is relying on a configuration item that I can't change. Is this a Laravel bug, perhaps?
BTW - I don't have 'web' set as the default guard when I get the error...
By default web guard given by laravel which used for web authentication based on session driver with the table users. Now you have created your own custom guards and you are using it. So your wish to keep that web guard. But if you remove, you might be facing some internal issue, so best you keep it and it's not going to be the performance issue too.

Integrating passport authentication in lumen

I am trying to integrate Passport authentication in Lumen (5.4.*) application using dusterio/lumen-passport https://github.com/dusterio/lumen-passport package.
I followed the steps till Installed routes, But when i try to access /oauth/token it throws 404 not found error
I am not sure what i am missing.
Can anyone help me out? Waiting for positive response.
be sure to register passport route and register auth config in bootstrap/app.php
add
Edit config/auth.php to suit your needs. A simple example:
return [
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \Your\UserModel::class
]
]];
Load the config in bootstrap/app.php since Lumen doesn't load config files automatically:
$app->configure('auth');
and
Dusterio\LumenPassport\LumenPassport::routes($this->app);
Although it is not recommended to edit vendor file , if you want I have a solution editing vendor file. Edit the below file as given below:
vendor/dusterio/lumen-passport/src/LumenPassport.php
Edit line 83 from
$callback->group(...
to
$callback->router->group(...

Laravel : Webhook class not found - Captain Hook

I'm trying to use captain-hook package to make webhook in my laravel app so that I can send a notification to a specific user when an eloquent event is fired.
I followed the package's documentation and put this code at the end of the method that will fire the event :
Webhook::create([
"url" => Input::get("url"),
"event" => "eloquent.saved: \App\DoctorRequest",
"tenant_id" => $user->id
]);
but I get this error :
FatalThrowableError in DoctorRequestController.php line 109:
Class 'App\Http\Controllers\Webhook' not found
How can I fix it ?
You haven't imported this in your controller.
Assuming you've added the Service Provider in your config/app.php file then you just need to import it at the top of your controller with:
use Webhook;
If you don't wish to import the Facade then you can reference it like this instead:
\Webhook::create([
"url" => Input::get("url"),
"event" => "eloquent.saved: \App\DoctorRequest",
"tenant_id" => $user->id
]);

restfullyii Defining Custom Routes

i bulit api using yii restful extension http://www.yiiframework.com/extension/restfullyii/
i want create api for login which accept username and password then return all user
information i think the correct way to do that is to create custom router after checking
extension documentation section Defining Custom Routes the author say i must create http verb combination (event name = 'req..\.render')
[POST] api/login/login
to create url i do this
main.php
array('<controller>/req.post.login', 'pattern'=>'api/<controller:\w+>', 'verb'=>'POST'),
login Controller :
public function restEvents()
{
$this->onRest('req.post.login.render', function() {
echo "sss";
//Custom logic for this route.
//Should output results.
$this->emitRest('req.render.json', [
[
'type'=>'raw',
'data'=>['active'=>true]
]
])
});
}
restEvents didn't fire any help ??
to create custom routing add this to controler and url will be http://css.local.com/index.php/api/controlername/test
$this->onRest('req.post.test.render',function() {
$this->emitRest('req.render.json', [
[
'type'=>'raw',
'data'=>data
]
]);
});