2015-06-11 26 views
7

Tôi đang sử dụng FOSRestBundle với Symfony 2 để triển khai REST API ở định dạng JSON.Làm thế nào để bạn tùy chỉnh định dạng ngoại lệ với FOSRestBundle và Symfony 2?

Tôi muốn tất cả các trường hợp ngoại lệ API để được trả lại trong một định dạng JSON cụ thể như thế này:

{ 
    "success": false, 
    "exception": { 
     "exceptionClass": "SomeNastyException", 
     "message": "A nasty exception occurred" 
    } 
} 

Làm thế nào để làm điều này?

Tôi đã cố gắng để fiddle với ExceptionController, nhưng nó có vẻ quá phức tạp để dễ dàng bị quá tải.

+0

bạn đã giải quyết này? Tôi gặp rắc rối tương tự –

Trả lời

10

Lưu ý: Tính năng này chỉ hoạt động đối với FOSResBundle < 2.0. Đối với FOSResBundle> = 2.0, vui lòng sử dụng Trình bình thường ngoại lệ, xem examples.

Bạn có thể viết trình xử lý trình bao bọc ngoại lệ tùy chỉnh như trong docs. Trong trường hợp của bạn:

<?php 
//AppBundle\Handler\MyExceptionWrapperHandler.php 
namespace AppBundle\Handler; 

use FOS\RestBundle\Util\ExceptionWrapper; 
use FOS\RestBundle\View\ExceptionWrapperHandlerInterface; 

class MyExceptionWrapperHandler implements ExceptionWrapperHandlerInterface { 

    public function wrap($data) 
    { 
     /** @var \Symfony\Component\Debug\Exception\FlattenException $exception */ 
     $exception = $data['exception']; 

     $newException = array(
      'success' => false, 
      'exception' => array(
       'exceptionClass' => $exception->getClass(), 
       'message' => $data['status_text'] 
      ) 
     ); 

     return $newException; 
    } 
} 

app/config/config.yml

fos_rest: 
    routing_loader: 
     default_format: json 

    view: 
     view_response_listener: force 
     exception_wrapper_handler: AppBundle\Handler\MyExceptionWrapperHandler 

    exception: 
      enabled:    true 

đáp ứng dụ:

{"success":false,"exception":{"exceptionClass":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"Not Found"}} 
+1

Tôi nghĩ câu trả lời này có thể không còn hợp lệ nữa. FOSRestBundle không còn bao gồm một lớp trình xử lý hoặc trình bao bọc trình bao bọc ngoại lệ từ những gì tôi có thể thấy. – JackalopeZero

+0

Điều này không được chấp nhận vì v2.0 của FOS Rest. Một ý tưởng khác? –

+0

Có, nó không được chấp nhận vì bây giờ nó phụ thuộc vào serializer của bạn.Vì vậy, bạn nên tìm cách làm thế nào để sửa đổi ngoại lệ bằng cách sử dụng serializer của bạn. Tôi sẽ thêm ví dụ nhưng tôi sợ câu trả lời sẽ quá lớn. Ví dụ: trình bình thường hóa cho JMSSerializerBundle https://github.com/FriendsOfSymfony/FOSRestBundle/blob/2.0/Serializer/Normalizer/ExceptionHandler.php –

5

tôi đã hạ cánh trên chủ đề này một số lần trên Trong vài ngày qua. Đối với bất kỳ ai khác trong trường hợp của tôi, nơi bạn đang sử dụng gói V2, bạn có thể tìm thấy tài nguyên sau đây khi nâng cấp FOSRestBundle hữu ích.

Nó bao gồm việc sử dụng bộ nối tiếp thay cho ngoại lệ ExceptionWrapperHandlerInterface.

https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md

  • Tùy chọn exception_wrapper_handler cấu hình đã được gỡ bỏ. Sử dụng bình thường để thay thế.

Trước:

config.yml

fos_rest: 
    view: 
     exception_wrapper_handler: AppBundle\ExceptionWrapperHandler 

Handler

namespace AppBundle; 

class ExceptionWrapperHandler implements ExceptionWrapperHandlerInterface 
{ 
    public function wrap($data) 
    { 
     return new ExceptionWrapper(array('status_code' => 'foo')); 
    } 
} 

Sau (nếu bạn sử dụng serializer Symfony):

services.yml

services: 
    app_bundle.exception_normalizer: 
     class: AppBundle\Normalizer\ExceptionNormalizer 
     tags: 
      - { name: serializer.normalizer } 

normalizer

namespace AppBundle\Normalizer; 

use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 

class ExceptionNormalizer implements NormalizerInterface 
{ 
    public function normalize($object, $format = null, array $context = array()) 
    { 
     return array('status_code' => 'foo'); 
    } 

    public function supportsNormalization($data, $format = null) 
    { 
     return $data instanceof \My\Exception; 
    } 
} 
Các vấn đề liên quan