/** * Throws an ValidationResponse * * it handles the JSON case and the HTML case * @param ValidatorException[] * @param const $format im moment nur JSON oder HTML * @return HTTPException */ public function validationResponse(\Psc\Form\ValidatorExceptionList $list, $format = ServiceResponse::JSON, MetadataGenerator $metadataGenerator = NULL) { if ($format === ServiceResponse::JSON) { $body = (object) array('errors' => array()); foreach ($list->getExceptions() as $validatorException) { $body->errors[$validatorException->field] = $validatorException->getMessage(); } return HTTPResponseException::BadRequest(json_encode($body), NULL, array('Content-Type' => 'application/json')); } else { $metadataGenerator = $metadataGenerator ?: new MetadataGenerator(); return HTTPException::BadRequest($list->getMessage(), $list, array_merge(array('Content-Type' => 'text/html'), $metadataGenerator->validationList($list)->toHeaders())); } }
/** * @return ServiceResponse */ public function route(ServiceRequest $serviceRequest) { try { $this->service = $this->findService($serviceRequest); return $this->service->route($serviceRequest); } catch (\Psc\Form\ValidatorException $e) { $this->logError($e, $this->debugLevel, 2); throw HTTPException::BadRequest(sprintf("Beim Validieren der Daten ist ein Fehler aufgetreten. %s", $e->getMessage()), $e, array_merge(array('Content-Type' => 'text/html'), $this->metadataGenerator->validationError($e)->toHeaders())); } catch (\Psc\Form\ValidatorExceptionList $list) { $this->logError($list, $this->debugLevel, 2); throw $this->err->validationResponse($list, $this->request->accepts('application/json') ? ServiceResponse::JSON : ServiceResponse::HTML, $this->metadataGenerator); } }
public static function provideServiceExceptions() { return array(array(\Psc\Net\HTTP\HTTPException::NotFound()), array(\Psc\Net\HTTP\HTTPException::BadRequest()), array(new \Exception('Manno')), array(new \Psc\Exception('I am faling'))); }