I am using the Drupal REST module. I have a custom module file I have created and it has a hook below
function magri_rest_user_presave($account) {
if (isset($account->field_region->value)) {
$taxonomy_query = \Drupal::entityQuery('taxonomy_term');
$taxonomy_query ->condition('vid', "region")
->condition('name', $account->field_region->value);
$tids = $taxonomy_query->execute();
$tid = array_values($tids)[0];
if($tid){
//set taxonomy ref field on user entity
$account->field_region_tax->target_id = $tid;
}else{
throw new NotFoundHttpException(t('Region not found!'));
}
}
The exception is thrown but it only gets printed in the logs:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: Region not found! in magri_rest_user_presave() (line 61 of /var/www/html/modules/custom/magri_rest/magri_rest.module).
Symfony\Component\HttpKernel\Exception\HttpException: Internal Server Error in Drupal\rest\Plugin\rest\resource\EntityResource->post() (line 198 of /var/www/html/core/modules/rest/src/Plugin/rest/resource/EntityResource.php).
I want something like below to be returned to the client:
{
"error":"Region not found!",
}
NotFoundException()
class? – Ismail Cherri Sep 19 '19 at 16:49