anlutro / laravel-4-smart-errors

Smarter error handling for Laravel 4
65 stars 13 forks source link

Extend template data #12

Closed tomvo closed 9 years ago

tomvo commented 10 years ago

How would I go about extending the data available in the template? I'd like to add information about the current loggedin user, the URL etc.

Thanks

anlutro commented 10 years ago

In the config, set 'error-view' => null to prevent the package from returning a view. Then, add your own error handler to app/start/global.php, but use pushError() instead of error(). For example:

App::pushError(function($exception) {
    if (App::runningInConsole() || Config::get('app.debug')) return;
    return Response::view('my-error-view', [...], 500);
});

This will preserve all logging, mailing etc. but let you return your own response.

anlutro commented 10 years ago

If you mean in the mail, that's currently not possible (though the URL should be showing), but I can probably find a way to implement that now that I think about it.

anlutro commented 10 years ago

I've added the ability to swap out the AppInfoGenerator via the IoC container, so you can extend and customize it to your needs. For convenience you can use the getExtraStrings method which will be added to the mail body. In your case you could return ['user' => Auth::user()->username] for example.

See this test for an example: https://github.com/anlutro/laravel-4-smart-errors/blob/ad6fc0a3f47d99113030c45ba80b2fff499de295/tests/functional/ErrorHandlingTest.php#L249-L265

tomvo commented 10 years ago

Thanks, I'll give this a go. However, the URL doesn't seem to show in the current version. It only shows the application url in the email subject.

anlutro commented 10 years ago

Make sure you're on the latest stable version. And URLs obviously won't show up for console commands.

tomvo commented 10 years ago

Yeah, I'm on dev-master. But no worries i'll tweak it with the custom generator. Which I can't get working actually, I put this in my global.php:

App::bind('anlutro\L4SmartErrors\AppInfoGenerator', 'CC\LaravelErrorsCustomGenerator');

This is the contents of this custom generator:

<?php

namespace CC;

use anlutro\L4SmartErrors\AppInfoGenerator as AppInfoGenerator

class LaravelErrorsCustomGenerator extends AppInfoGenerator {
    public function getExtraStrings()
    {
        if(!Auth::guest()){
            return ['user' => Auth::user()->username];  
        }else{
            return ['user' => 'Unknown'];   
        }

    }
}

I'm getting a blank page when an error occurs?

anlutro commented 10 years ago

If that's your entire code then you still need to import the Auth facade class.

tomvo commented 10 years ago

Mmm, I'm also getting a error using just this:

<?php

namespace CC;

use anlutro\L4SmartErrors\AppInfoGenerator as AppInfoGenerator

class LaravelErrorsCustomGenerator extends AppInfoGenerator {
    public function getExtraStrings()
    {
        return ['test' => 'hello'];
    }
}

Error in exception handler.

anlutro commented 10 years ago

I'm able to make it work just fine with a fresh installation of laravel/laravel, so the problem is likely somewhere in your own codebase.

anlutro commented 10 years ago

Another thing you can do is temporarily set app.debug to true in the config to get at least the exception message.

tomvo commented 10 years ago

Thanks. I'm causing the error on purpose to test the email out. debug is already set to true. Also, commenting out the app::bind() method fixes the problem right away.

I'll have another fresh look tomorrow morning. thanks!