/** * Create new HttpResponse from exception. * * @param \Exception $exception * * @return HttpResponse */ protected function createHttpResponseFromException(\Exception $exception) { $httpResponse = HttpResponse::create(); $json = []; $json['jsonrpc'] = '2.0'; $json['error'] = []; if ($exception instanceof Exceptions\ErrorException) { $json['error']['code'] = $exception->getCode(); $json['error']['message'] = $exception->getMessage(); if ($exception->getData()) { $json['error']['data'] = $exception->getData(); } $json['id'] = $exception->getId(); } else { $json['error']['code'] = -32603; $json['error']['message'] = 'Internal error'; $json['id'] = null; } $httpResponse->headers->set('Content-Type', 'application/json'); $httpResponse->setContent(json_encode($json)); $httpResponse->setStatusCode(500); return $httpResponse; }
public function solve(Request $request, \Exception $exception) { if (!$exception instanceof ServiceNotFoundException) { return null; } try { $finder = new ObjectFinder(); $container = $finder->find('Symfony\\Component\\DependencyInjection\\Container'); $ids = $container->getServiceIds(); $searchedId = $exception->getId(); $similarity = array(); foreach ($ids as $id) { $percentage = 0.0; similar_text($id, $searchedId, $percentage); if ($percentage >= 90.0) { $similarity[$id] = $percentage; } } arsort($similarity); if (!$similarity) { return null; } return new SimpleBlockSolution('Do you have a typo in the service name?', 'Solution:service_name_typo.html.php', array('serviceIds' => $similarity)); } catch (\Exception $ex) { return null; } }
public function notify(\Exception $exception) { if (!$exception instanceof ServiceNotFoundException) { return; } $serviceId = $exception->getId(); $guessedFqcn = $this->guessFqcn($serviceId); $definition = new Definition(); $definition->setClass($guessedFqcn); $containerBuilder = new ContainerBuilder(); $containerBuilder->addDefinitions([$serviceId => $definition]); $dumper = new YamlDumper($containerBuilder); $result = $dumper->dump(); $message = sprintf('Service `%s` missing. Define it in your services.yml:', $serviceId); $this->output->writeln('--- ' . $message . PHP_EOL); $this->output->write($result, true); $errorMessages = ['Service ' . $serviceId . ' was not found.']; $formatter = new FormatterHelper(); $formattedBlock = $formatter->formatBlock($errorMessages, 'error', true); $this->output->writeln(''); $this->output->writeln($formattedBlock); $this->output->writeln(''); $question = sprintf('<question>Do you want to create a specification for %s? (Y/n)</question>', $guessedFqcn); $dialog = new DialogHelper(); if ($dialog->askConfirmation($this->output, $question, true)) { $this->specRunner->runDescCommand($guessedFqcn); } }
/** * generates the default error output * * @author Daniel Sherman * @param \Exception $e The exception thats needs to be outputted * @return string the default output */ public function generateDefaultOutput(\Exception $e) { $message = 'Issue: '; if ($e instanceof ExceptionInterface) { $message .= $e->getId(); } $interface = php_sapi_name(); if (substr($interface, 0, 3) != 'cli') { // this can't really be unit tested tested // it should be noted that the response code will be ignored // if data has already been sent to the browser. http_response_code(500); } return $message; }