Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error content not displayed #1

Closed
Wikiki opened this issue Jan 9, 2017 · 5 comments
Closed

Error content not displayed #1

Wikiki opened this issue Jan 9, 2017 · 5 comments

Comments

@Wikiki
Copy link

Wikiki commented Jan 9, 2017

Hi,

I set this middleware at the top of the stack, The errors and exception are well intercepted (I saw them into my Logger) but my response content is not displayed.

I use the following code:
( new Middlewares\ErrorHandler( 'App::errorHandler' ) )->catchExceptions( true ),

And my handler is like

static public function errorHandler( ServerRequestInterface $request ) {
    //Get the error info using the "error" attribute
    $error = $request->getAttribute( 'error' );
    $response = ( new Response() )->withStatus( $error[ 'status_code' ] );

    switch ( $error['status_code'] ) {
      case 404:
        App::$view->setView( 'errorNotFound' );
        $output = App::$view->__invoke();
        $response->getBody()->write( $output );

      case 503:
        App::$view->setView( 'maintenance' );
        $output = App::$view->__invoke();
        $response->getBody()->write( $output );

      default:
        App::$logger->debug( $error[ 'exception' ]->getMessage() );
        App::$view->setView( 'errorServer' );
        $output = App::$view->__invoke();
        $response->getBody()->write( $output );
    }

    return $response;
  }

What am I doing wrong ?

@Wikiki
Copy link
Author

Wikiki commented Jan 9, 2017

Just for information I'm using middleman as dispatcher. And I'm facing the same issue using the dispatcher from Middlewares\Utils

@oscarotero
Copy link
Member

Middleman is using an old version of the PSR-15 interface (0.2 instead the current 0.4). I recommend using the dispatcher from Middleware\Utils.
Anyway, I suspect this is a bug in your code:

  • You should add a break after each case in the switch
  • If you don't see the response, are you sure the body has content?
  • Is being the response emitted?

@Wikiki
Copy link
Author

Wikiki commented Jan 10, 2017

Hi,

I used the dispatcher from Middleware\Utils but same result. I've already added a break after each case (not in the version I copied / pasted here sorry)
If I put a var_dump( $response->getBody() ); exit( 1); the page is well displayed.

Into my app I just do:
$dispatcher = new Middlewares\Utils\Dispatcher( $middlewares );
$response = App::$dispatcher->dispatch( ServerRequestFactory::fromGlobals() );

Do I have to do anything else to emit the response ? I thought the dispatcher handled that.

@oscarotero
Copy link
Member

Nop, the dispatcher returns the response but does not output the content to the browser. If you're using diactoros, it provides some emitters, depending of your needs. For example:

$emitter = new Zend\Diactoros\Response\SapiEmitter();
$emitter->emit($response);

@Wikiki
Copy link
Author

Wikiki commented Jan 10, 2017

Thank you for all your job and for your answer.

@Wikiki Wikiki closed this as completed Jan 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants