0

I'm just jump into laravel, this is my first project with.

Now i'm trying to add new exception to handler stack, but for sorry it's not working and i don't know why.

here is my exception class

namespace Lib\Modules\Users\Exceptions;

use Lib\Abstracts\AbstractException;

class ConnotCreateUserException extends AbstractException {

}

Here is my abstract

namespace Lib\Abstracts;

class AbstractException extends \Exception {

}

and here is my error registration inside app/start/global.php

App::error(function(Exception $exception, $code)
{
    Log::error($exception);
});

App::error(function(Lib\Modules\Users\Exceptions\ConnotCreateUserException $exception){
   return 'Sorry! Something is wrong with this account!';
});

when i throw the exception, i got blank page. but i'm sure it's handled as type of "\Exception" exception.

Thanks in advance

1 Answer 1

1

As the code looked fine, I just did some tests with it and it works for me, just make sure your namespaces are being loaded correctly by doing:

composer dump-autoload

But, anyway, here's what I did:

1) Created a PSR-4 autoloading for it:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "Lib\\" : "app/App/Lib"
    }
},

2) Create the files using your very content:

app/App/Lib/Abstracts/AbstractException.php
app/App/Lib/Modules/Users/Exceptions/ConnotCreateUserException.php

3) executed

composer dump-autoload

4) opened vendor/composer/autoload_psr4.php just to make sure it was there:

return array(
    'Lib\\' => array($baseDir . '/app/App/Lib'),
);

5) Added this all in my routes.php file:

App::error(function(Exception $exception, $code)
{
    Log::error($exception);
});

App::error(function(Lib\Modules\Users\Exceptions\ConnotCreateUserException $exception){
   return 'Sorry! Something is wrong with this account!';
});

Route::get('/', function()
{
    throw new Lib\Modules\Users\Exceptions\ConnotCreateUserException("We should not get this message!", 1);
});

6) Hit:

http://server.dev/

And got the message

Sorry! Something is wrong with this account!
Sign up to request clarification or add additional context in comments.

3 Comments

i don't why it's not working for me! however, i don't use psr-4 but psr-0 instead. do you think this have a relation to my problem? my exception file located in: app/Lib/Modules/Users/Exceptions/ConnotCreateUserException.php and my composer.json autoload is: "psr-0": { "Lib": "app" }. all other classes working fine but this exception. i tried to dump-autoload form composer but same results.
PSR-0 or 4, doesn't matter, but you're not showin all of your code, so it's hard to say. How are you raising that exception? Try to add a root slash () to the namespaced class, like: \Lib\Modules\Users\Exceptions\ConnotCreateUserException, sometimes it's just a namespace conflict and it happens a lot in exception handling.
i had install a fresh version of laravel and move all old files to it. and guess what! it's worked. however, currently i don't have enough time to dig deep in this issue, but i will when i have time. i will not let it go in peace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.