0

I'm using Laravel 4's App::error class to catch Sentry exceptions throughout my application and pass the data back to the template using the withErrors() function.

Simple route:

routes.php

Route::post('/login...

...

$credentials = array(
    'email'    => Input::get('email'),
    'password' => Input::get('password') 
);

$user = Sentry::authenticate($credentials);

// Exception thrown...

Then to catch the exceptions:

exceptions.php

App::error(function(Cartalyst\Sentry\Users\WrongPasswordException $e) {
    return Redirect::back()->withErrors(array('failed' => 'Email or password is incorrect'))->withInput();
});

In the view:

/views/login/login.blade.php

@if ($errors->has('failed'))
    <strong>{{ $errors->first('failed') }}</strong>
@endif

The problem is, when you refresh the page after a failed log in attempt these errors persist so you see them twice. Refreshing a second time, they have cleared. Same goes for the input (passed with withInput()).

If the errors are caught within the route (rather than in App:error), everything works as normal. Should I be clearing the stored data manually using the App::error methods?

Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
steveneaston
  • 259
  • 1
  • 4
  • 10

1 Answers1

0

I always use Session::flash() to display errors. Flash will (for one request) set (and automatically unset) data into your session. So you could go like

App::error(function(Cartalyst\Sentry\Users\WrongPasswordException $e) {
    Session::flash('error', 'Email or password is incorrect.');
    return Redirect::back()->withInput();
});

and catch this in your view:

@if($message = Session::get('success'))
    <div class="alert-box success">
        {{ $message }}
    </div>
@endif

@if($message = Session::get('error'))
    <div class="alert-box alert">
        {{ $message }}
    </div>
@endif

On a related note, I would suggest to follow the usual try-catch notation like so:

try {
    // do things that might throw an Exception here...  

} catch(Cartalyst\Sentry\Users\UserExistsException $e) {
    // catch the Exception...

    Session::flash('error', Lang::get('register.user_already_exists'));
    return Redirect::action('RegisterController@getIndex')->withInput();

}

... because what you're currently doing with App::error() is probably a little more cumbersome than that.

Harti
  • 1,480
  • 12
  • 16