Skip to content

getting 'The HTTP status code "0" is not valid' exception #418

Closed
arashdalir opened this Issue Apr 3, 2013 · 2 comments

2 participants

@arashdalir

Hi everyone!
I'm using FOSRestBundle to manage a profile edit form and am getting

The HTTP status code "0" is not valid

when trying to send redirection response to the client.

I've configured fos_rest as followed:

fos_rest:
    format_listener:
        default_priorities: ['json', "xml", html, '*/*']
    view:
        formats:
            xml: true
            json: true
        templating_formats:
            html: true
        force_redirects:
            html: false
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
        view_response_listener: 'force'
    service:
        serializer: jms_serializer.serializer
    serializer:
        version: ~
        groups: []

my respective action function is as followed:

public function partialEditAction(Request $request)
    {
        $user = $this->authenticate();
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->container->get('event_dispatcher');

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }

        $action = $request->get('part');

        $type = null;
        switch ($action){
        case 'personalInfo':
            $type = new PersonalInfoFormType();
            break;

        case 'email':
            $type = new EmailFormType();
            break;

        case 'password':
            $type = new PasswordFormType();
            break;
        }

        $form = $this->createForm($type, $user);
        //$form->set
        //$form->setData($user);

        if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid()) {
                /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
                $userManager = $this->container->get('fos_user.user_manager');

                //$event = new FormEvent($form, $request);
                //$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);

                $userManager->updateUser($user);

                //if (null === $response = $event->getResponse())
                {
                    $view = $this->routeRedirectView('fos_user_profile_show', array(), Codes::HTTP_SEE_OTHER);
                    //$view->setStatusCode(Codes::HTTP_NO_CONTENT);
                }

                //$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

                return $this->handleView($view);
            }
        }

        $view = $this->view();
        $view->setData( array('form' => $form) );
        $view->setTemplate('TaraUserBundle:Profile:form.html.twig');

        return $this->handleView($view);
    }

as you can see, I'm trying to create a routeRedirectResponse with Codes::HTTP_SEE_OTHER as the status code and fos_user_profile_show as the route name. I'm trying to read the response later on the client side and according to the status code (204, 200, etc.) do an action. if the response is HTTP_SEE_OTHER, I'd be reading the location header value to find the redirection location and handle the request manually...
but FOSRestBundle keeps throwing the "InvalidArgumentException":

The HTTP status code "0" is not valid.

500 Internal Server Error - InvalidArgumentException

I traced the error to FOS\RestBundle\View\ViewHandler.php line: 274 :

        $code = isset($this->forceRedirects[$format])
            ? $this->forceRedirects[$format] : $this->getStatusCode($view, $content);

        $response->setStatusCode($code);

this line checks the value of my fos_rest config, find the force_redirects value for html format to be "false" and simply replaces my given status_code with a "false" value; which later on results in the http status code to be "0".

I'm not sure if this means that I have misconfigured the bundle or this could actually be a bug in the system. Could anyone please advise me on this matter?

@lsmith77
FriendsOfSymfony member
lsmith77 commented Apr 4, 2013

maybe there is a bug in your routeRedirectView method?

@lsmith77
FriendsOfSymfony member

ping

@lsmith77 lsmith77 closed this Aug 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.