2012-05-16 13 views
17

Sto cercando di generare eccezioni e sto facendo il seguente:Symfony2 ed eccezione gettando errore

use Symfony\Component\HttpKernel\Exception\HttpNotFoundException; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

sto poi li utilizzano nel seguente modo:

throw new HttpNotFoundException("Page not found"); 
    throw $this->createNotFoundException('The product does not exist'); 

però che sto ottenendo gli errori come HttpNotFoundException non trovata, ecc.

È questo il modo migliore per generare eccezioni?

+1

È normale buttarli come primo esempio, throw new Exception ('Messaggio'); Finché hai importato la classe di eccezioni come sembra aver fatto con una dichiarazione d'uso, dovrebbe funzionare. Forse più di ciò che non stai mostrando - puoi pubblicare le intestazioni di classe e la traccia dello stack delle eccezioni? L'errore – PorridgeBear

+0

è: Errore irreversibile: Classe 'Rest \ UserBundle \ Controller \ HttpNotFoundException' non trovato in /Users/jinni/Sites/symfony.com/src/Rest/UserBundle/Controller/DefaultController.php – jini

+0

Ho incluso usare Symfony \ Component \ HttpKernel \ Exception in cima – jini

risposta

45

Prova:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 

e

throw new NotFoundHttpException("Page not found"); 

penso che hai un po 'indietro :-)

+0

grazie Chris ... – jini

+0

grazie @Chris, ha funzionato come un fascino – Stevanicus

+0

Non invia lo stato 404 – Volatil3

9

Se la sua in un controllore, si può fare in questo modo:

throw $this->createNotFoundException('Unable to find entity.'); 
25

in qualsiasi controllo ler è possibile utilizzare questo per risposta 404 HTTP all'interno Symfony

throw $this->createNotFoundException('Sorry not existing'); 

stessa

throw new NotFoundHttpException('Sorry not existing!'); 

o questo per 500 codice di risposta HTTP

throw $this->createException('Something went wrong'); 

stessa

throw new \Exception('Something went wrong!'); 

o

//in your controller 
$response = new Response(); 
$response->setStatusCode(500); 
return $response; 

o questo è per qualsiasi tipo di errori

throw new Symfony\Component\HttpKernel\Exception\HttpException(500, "Some description"); 

anche ... Per Eccezione personalizzatoyou can flow this URL

+1

meglio .. ha detto nuff. – JohnnyQ

+2

@ hassan-magdy, non riesco a trovare il metodo "createException (...)" in symfony 2.3, 2.7 o 3.0 in qualsiasi classe. Sei sicuro che funzioni? –

+0

@NunoPereira La soluzione migliore dovrebbe essere lanciare una nuova [HTTPException] (http://api.symfony.com/3.2/Symfony/Component/HttpKernel/Exception/HttpException.html#method___construct), come descritto nell'ultimo esempio sopra. – sentenza

0

Nel controllore, si può semplicemente do:

public function someAction() 
{ 
    // ... 

    // Tested, and the user does not have permissions 
    throw $this->createAccessDeniedException("You don't have access to this page!"); 

    // or tested and didn't found the product 
    throw $this->createNotFoundException('The product does not exist'); 

    // ... 
} 

In questa situazione, non è necessario includere use Symfony\Component\HttpKernel\Exception\HttpNotFoundException; nella parte superiore. Il motivo è che non stai usando la classe direttamente, come usare il costruttore.

Al di fuori del controller, è necessario indicare dove si trova la classe e generare un'eccezione come si farebbe normalmente.Come questo:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 

// ... 

// Something is missing 
throw new HttpNotFoundException('The product does not exist'); 

o

use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

// ... 

// Permissions were denied 
throw new AccessDeniedException("You don't have access to this page!"); 
Problemi correlati