【问题标题】:Error 500 with FOSRestBundle and BehatFOSRestBundle 和 Behat 出现错误 500
【发布时间】:2014-07-05 20:28:15
【问题描述】:

我正在尝试使用 Behat 测试我的 API,但每次响应都会得到 500。

API 在 Behat 之外也能正常工作。

这是我的 config.yml 文件的一部分:

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    routing_loader:
        include_format: false
    format_listener:
        rules:
            - { priorities: ['json','html'], fallback_format: json }
    view:
        view_response_listener: true
        failed_validation: 422
    serializer:
        serialize_null: true
    disable_csrf_role : IS_AUTHENTICATED_ANONYMOUSLY

对于我的控制器,我选择使用 get/cget/... 语法扩展 FOSRestController。 这是一个动作示例。

<?php

namespace RcpIndex\Bundle\CoreBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\Route;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;

use RcpIndex\Bundle\CoreBundle\Form\RcpType;

/**
 * @package RcpIndex\Bundle\CoreBundle\Controller
 */
class RcpController extends FOSRestController implements ClassResourceInterface
{
    use PaginatorTrait;

    /**
     * Get rcp list
     *
     * @param ParamFetcher $paramFetcher
     *
     * @return \FOS\RestBundle\View\View
     *
     * @QueryParam(name="page", requirements="\d+", default="1", description="Current page index")
     * @QueryParam(name="per_page", requirements="\d+", default="50", description="Number of elements displayed per page")
     *
     * @ApiDoc(
     *  section="Rcp",
     *   description="Get rcp list",
     *   output="RcpIndex\Bundle\CoreBundle\Entity\Rcp",
     *   statusCodes={
     *     200="OK",
     *     400="Bad request"
     *   }
     * )
     */
    public function cgetAction(ParamFetcher $paramFetcher)
    {
        list($offset, $limit) = $this->getStartAndLimitFromParams($paramFetcher);
        $entities = $this->get('rcpindex.manager.rcp')->findAllInRange($offset, $limit);

        return $this->view($entities, 200);
    }
//...

然后是我的 Behat 功能。我使用 Behat\MinkExtension 和 Sanpi\Behatch 作为上下文提供者。

Feature: Rcplist
    Scenario: Get empty list
        Given I add "CONTENT_TYPE" header equal to "application/json"
        When I send a GET request on "/api/1.0/rcps"
        Then the response status code should be 200

我收到以下错误:“当前响应状态代码为 500,但预期为 200。”

我尝试调试它,但代码运行良好,$this->view() 方法没有抛出可捕获的异常(经过一番搜索,View::create() 方法运行良好)。

我猜“cgetAction”返回是好的,但它可能是响应的处理方式。 我对 Symfony 响应处理的了解不足,无法调试。

任何想法在哪里看(或者更好,任何解决方案?)

【问题讨论】:

  • 检查 app/logs/ 文件看是否是 Symfony 错误。并显示您的上下文文件。

标签: symfony response behat fosrestbundle


【解决方案1】:

我也遇到过同样的问题。要检查引发错误的原因,在 FOS/RestBundle 包 vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Controller/ExceptionController.phpExceptionController 中,在 showAction() 方法的 catch 部分中,尝试直接转储异常,您将获得更多详细信息:

    } catch (\Exception $e) {
        var_dump($e);
        die();
        $message = 'An Exception was thrown while handling: ';
        $message.= $this->getExceptionMessage($exception);
        $response = new Response($message, Codes::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders());
    }

【讨论】:

    【解决方案2】:

    我在使用 Symfony 4 时遇到了同样的问题,因为我忘记在 .env.test 中设置环境变量

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2011-07-07
      • 2013-11-18
      相关资源
      最近更新 更多