我在Symfony控制器中有类似的内容:
try {
$entity = $this->getDoctrine()->getRepository(Entity::class)->findOneBy(['uuid' => $uuid]);
} catch(\Exception $ex) {
// die('Invalid uuid'); <- this works
throw $this->createNotFoundException('Resource not found...'); // this doesn't work
}
if(!$campaign) {
// this works if the uuid has a valid format but doesn't exist in db
throw $this->createNotFoundException($this->get('translator')->trans(Utils::NOT_FOUND_STRING));
}
如果用户打开带有无效UUID的页面,我想显示错误404,而不是500。
捕获了异常,因为如果我放置die(),它将起作用。但是有了throw $this->createNotFoundException(
,我得到了:
ConversionException
无法将数据库值“ 55012772-4801-4e26-8 ...”转换为主义类型uuid_binary