/** * @dataProvider provideServiceExceptions */ public function testHandlerReturnsResponse_evenIfException(\Exception $exception) { $this->setServiceIsResponsible($this->svc, TRUE); $this->expectServiceGetsRouted($this->svc); $this->setConverterConverts(Response::create(200, 'mybody')); $this->assertInstanceOf('Psc\\Net\\HTTP\\Response', $response = $this->handler->handle($this->doublesManager->createHTTPRequest('GET', '/episodes/8/form'))); $this->assertGreaterThan(0, $response->getCode()); }
/** * @return Response */ public function handle(Request $request) { $this->log($request->debug()); $this->request = $request; try { $serviceRequest = $this->createServiceRequest($this->request); $serviceResponse = $this->route($serviceRequest); $this->response = $this->convertResponse($serviceResponse); } catch (HTTPResponseException $e) { $this->response = Response::create($e->getCode(), $e->getResponseBody(), $e->getHeaders()); } catch (HTTPException $e) { // die darstellung sollte eigentlich woanders sein, nicht hier // eigentlich müssten wir hier auch request->content-type auswerten und response umwandeln $this->response = Response::create($e->getCode(), sprintf("%s\n\n%s\n%s", $e->getResponseBody(), $request->getResource(), $this->getDebugInfo()), $e->getHeaders()); } catch (NoServiceFoundException $e) { $this->logError($e, $this->debugLevel, 1); $e->setCode(404); $this->response = Response::create(404, sprintf("Es wurde kein Service für: %s gefunden\n\n%s", $request->getResource(), $this->getDebugInfo())); } catch (\Psc\CMS\Service\ControllerRouteException $e) { $this->logError($e, $this->debugLevel, 5); // nicht 1 da das mit dem Klassennamen ja schon "interna" sind $e->setCode(404); $this->response = Response::create(404, sprintf($e->getMessage() . "\n\n%s", $this->getDebugInfo())); } catch (\Exception $e) { $this->logError($e, $this->debugLevel, 1); $this->response = Response::create(500, sprintf("Es ist ein Fehler aufgetreten. URL: %s%s\n\n%s", $request->getResource(), $this->debugLevel >= 5 ? "\nFehler: " . $e->getMessage() : NULL, $this->getDebugInfo()), $this->getErrorMessageHeaders($e)); } if (isset($e)) { // oder halt code >= 400 if (!$this->isIgnoredError($e)) { $contextInfo = 'im RequestHandler. ' . "\n"; $contextInfo .= ' ' . $request->getMethod() . ' /' . implode('/', $request->getParts()) . "\n"; $contextInfo .= ' Accept: ' . $request->getHeaderField('Accept') . "\n"; $contextInfo .= ' Referer: ' . $request->getReferer() . "\n"; $contextInfo .= ' User-Agent: ' . $request->getUserAgent(); if (isset($this->contextInfo)) { $contextInfo .= "\n" . $this->contextInfo; } $contextInfo .= "\n\n" . $this->dumpRequest($request); \Psc\PSC::getEnvironment()->getErrorHandler()->handleCaughtException($e, $contextInfo); } } return $this->response; }
protected function createResponse($body) { return Response::create(200, $body, $this->headers); }
/** * @return Psc\Net\HTTP\Response */ public function handle(Request $request) { try { // RequestHandler aufrufen, der muss gehen und eine response zurückgeben $this->response = $this->requestHandler->handle($request); if ($this->response instanceof Response) { return $this->response; } else { throw new \Psc\Exception('interner Fehler: RequestHandler gibt keine passende Response zurück'); } } catch (\Exception $e) { /* dies ist nur das Ausnahmen / Ausnahmen Request-Handling. Eine 500 sollte im normalen Betrieb nie vorkommena sondern der RequestHandler sollte immer eine Response umwandeln */ if ($this->debugLevel >= 10) { throw $e; // rethrow denn wir machen das schon im RequestHandler alles } else { return Response::create(500, 'Es ist ein Interner HTTP-Handler Fehler (Frontcontroller) aufgetreten: ' . $e->getMessage()); } } }
<?php use Psc\Net\HTTP\Response; $response = Response::create(200, 'continuation(0)', array('Content-Type' => 'text/html; charset=utf-8')); $hasOutput = function ($tick) { return $tick % 2 == 0; }; $getOutput = function ($tick) { return sprintf("continuation(%d)<br />\n", floor($tick / 2)); }; $response->setOutputClosure(function () use($hasOutput, $getOutput) { while (ob_get_level() > 0) { ob_end_flush(); } // this will force internet explorer to not wait for the ouput when the tab / windows is NEW (reload always works as expected) print str_repeat(" ", 256); flush(); // up it goes print "starting upload<br />\n"; flush(); $ms = 400; for ($i = 1; $i <= 20; $i++) { usleep($ms * 1000); if ($hasOutput($i)) { print $getOutput($i); } flush(); } }); $response->output();
<?php try { /** * Ein Script was den Request aus den Globals Inferred und serialisiert ausgibt */ $request = \Psc\Net\HTTP\Request::infer(); print serialize($request); } catch (\Exception $e) { $message = $e->getMessage() . ' ' . $e->getFile() . ':' . $e->getLine(); $message = str_replace(array("\n", "\"r"), ' ', $message); $response = \Psc\Net\HTTP\Response::create(500, (string) $e, array('X-Psc-CMS-Error' => 'true', 'X-Psc-CMS-Error-Message' => $message)); $response->output(); }