Throwing Exceptions in Eloquent ORM Observers

1

I have the following code in my model:

public static function boot(){
    parent::boot();

    // Não deixa excluir caso possua registros vinculados.
    static::deleting(function($content_area){
        if($total = $content_area->contents->count() > 0){
            //throw new Exception("Essa área não pode ser removida, ela possui {$total} conteútos vinculados.");
            return App::abort(403, "Essa área não pode ser removida, ela possui {$total} conteútos vinculados.");
        }
    });
}

In my controller I have the following:

public function destroy($id){
    $content_area = ContentArea::find($id);

    try {
        $content_area->delete();
    } catch (Exception $e) {
        return Response::json(
            [
                'success' => 'false',
                'message' => $e->getMessage(),
                'messageType' => 'error'
            ]
        );
    }

    return Response::json(['success' => false]);
}

Theoretically, during log deletion, it should throw the exception if the condition is true within the observer.

The problem is that it shows the Laravel exceptions screen and kills the application. I did not want this to happen, I wanted it to just return the message for me to display to the user. Does anyone know why this is happening?

    
asked by anonymous 12.05.2014 / 18:04

1 answer

2

If you are using namespaces, you may well need to inform PHP that your exception is not in the current namespace, but in the root:

catch (\Exception $e)
    
22.05.2014 / 17:51