2017-06-05 11 views
0

特定のケースに基づいてストライプがスローする可能性があるすべての例外に対するStripe例外ラッパーがあります。私はその例外がスローされるイベントリスナーを登録しようとしていますが、リスナーが解雇されたようには見えません。例外リスナーを登録するsymfony3

jsonレスポンスでイベントを返す代わりに、例外が500をスローしてスタックトレースをレンダリングします。私が紛失しているものがありますか?

// AppBundle/Exceptions/StripePaymentException.php 
namespace AppBundle\Exceptions; 

use Exception; 

class StripePaymentException extends Exception { 

    public $response; 
    public $message; 

    public function __construct($message, $response, $code = 0, Exception $previous = null) 
    { 
     parent::__construct($message, $code); 

     $this->message = $message; 
     $this->response = $response; 
    } 

    public function getResponse() 
    { 
     return $this->$response; 
    } 

} 

// AppBundle/EventListener/StripePaymentExceptionListener.php 
namespace AppBundle\EventListener; 

use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 

class StripePaymentExceptionListener 
{ 
    public function onKernelException(GetResponseForExceptionEvent $event) 
    { 
     $exception = $event->getException(); 
     $data = $exception->getResponse(); 
     $response = new JsonResponse($data); 
     $event->setResponse($response); 
    } 
} 

// services.yml 
app.stripe_payment_exception_listener: 
class: AppBundle\EventListener\StripePaymentExceptionListener 
    tags: 
     - { name: kernel.event_listener, event: kernel.exception, priority: 200 } 
+0

どのエラーが正確に行うかあなたは検索しますか? –

+0

@AlessandroMinoccheri例外がスローされますが、(リスナーを介して)期待どおりの方法でフォーマットされません。それは単にレスポンスを返すのではなく、500ページを投げて新しいページをレンダリングしています。 – mashedpotatoes

答えて

0

カスタムリスナーにはpriorityの設定があります。あなたが解雇されている別のリスナーがの前にある場合は、の前に、これは実際に例外を返すものではありません。

// services.yml 
app.stripe_payment_exception_listener: 
class: AppBundle\EventListener\StripePaymentExceptionListener 
    tags: 
     - { name: kernel.event_listener, event: kernel.exception, priority: 1 } 

をまたは代わりに、あなたは、キャッチした例外の種類に基づいて、フォークにあなたの「標準」のExceptionListenerを変更することもできます:

あなたはpriorityここで変更される可能性が

if(instanceof $exception STRIPE_EXCEPTION_CLASS) 
    $data = $exception->getResponse(); 
    $response = new JsonResponse($data); 
    $event->setResponse($response); 
} 
else { 
    //"normal" behavior here 
} 

...またはベースタイプ上のAcceptヘッダー

//if you're accepting json, return json 
if($accept->has('application/json')) { 
    $response = new Response(json_encode($data, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); 
    $response->headers->set('Content-Type', 'application/json; charset=utf-8'); 
    $event->setResponse($response); 
} 
//if accepting xml, return xml.. 
elseif($request->isXmlHttpRequest()) { 
    $xml = new \SimpleXMLElement('<exception/>'); 
    $data = array_flip($data); 
    array_walk_recursive($data, array ($xml, 'addChild')); 
    $response = new Response($xml->asXML()); 
    $response->headers->set('Content-Type', 'text/xml; charset=utf-8'); 
    $event->setResponse($response); 
} 
+0

私は優先度を変更しようとしました。 'php bin/console debug:event-dispatcher kernel.exception'を実行すると、StripePaymentExceptionListenerが最初の順番になります。ただし、例外がスローされたときにリスナーに何も表示されないようです(xdebugブレークポイントとvar_dumpを配置しようとしましたが、どちらも機能しません)。 – mashedpotatoes

+0

'setResponse'を呼び出した後に' return'文を追加しようとしましたか? –

関連する問題