how throw a custom exception in JSONAPI? In D8 i'm trying to throw custom exceptions for extra validations that i need to do before the content is saved in my custom entity, but when i do the validations in the presave method and throw a normal Exception:
public function preSave(EntityStorageInterface $storage) { 
  throw new \Exception(&quot;The content is duplicated&quot;, 409);
}

The response of JSONAPI is code 500 Internal Server Error and the response:
{
&quot;jsonapi&quot;: {
    &quot;version&quot;: &quot;1.0&quot;,
    &quot;meta&quot;: {
        &quot;links&quot;: {
            &quot;self&quot;: {
                &quot;href&quot;: &quot;http://jsonapi.org/format/1.0/&quot;
            }
        }
    }
},
&quot;errors&quot;: [
    {
        &quot;title&quot;: &quot;Internal Server Error&quot;,
        &quot;status&quot;: &quot;500&quot;,
        &quot;detail&quot;: &quot;The content is duplicated&quot;,
        &quot;links&quot;: {
            &quot;via&quot;: {
                &quot;href&quot;: &quot;http://site.docksal/jsonapi/content_entity/content_entity&quot;
            },
            &quot;info&quot;: {
                &quot;href&quot;: &quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1&quot;
            }
        }
    }
]

}
how can i control de error Code, title, status and detail.
thanks  @4k4, I solved it using customs constraints in my custom entity, JSONAPI mapping well the error and the admin UI works perfectly with that.
like @kiamlaluno said, i try to explain the solution using constraints:
I want to response in the JSONAPI Request that the content is already exists but with a different code, no 500. So when i use constraints i can validate if content already exist or not and response if is a valid request (TRUE / FALSE), this constraints works in the JSONAPI Responses and in the Drupal Admin UI.
So you need to define the constraint in the folder
&quot;my_module/src/Plugin/Validation/Constraint&quot; and create 2 files, for my example i need that the content is unique. so my files are:
UniqueProfileContentConstraint.php
&lt;?php

namespace Drupal\my_module\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;

/**
 * Plugin implementation of the 'unique_entry_constraint'.
 *
 * @Constraint(
 *   id = &quot;unique_profile_content_constraint&quot;,
 *   label = @Translation(&quot;Unique entry constraint&quot;, context = &quot;Validation&quot;),
 * )
 */
class UniqueProfileContentConstraint extends Constraint
{

    // The message that will be shown if the value is not unique.
    public $notUnique = 'This content already exists';

}

And the validator:
UniqueProfileContentConstraintValidator.php
&lt;?php

namespace Drupal\ztv_content\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Validates the UniqueInteger constraint.
 */
class UniqueProfileContentConstraintValidator extends ConstraintValidator
{

    /**
     * {@inheritdoc}
     */
    public function validate($entity, Constraint $constraint) {
      // Next check if the value is unique.
      if (!$this-&gt;isUnique($entity)) {
          $this-&gt;context-&gt;addViolation($constraint-&gt;notUnique);
      }
    }

    private function isUnique($entity)
    {
      if ($entity-&gt;isNew()) {
        $query = \Drupal::entityQuery('profile_content_entity')
          -&gt;condition('profile_id', $entity-&gt;get('profile_id')-&gt;getString())
          -&gt;condition('video_id', $entity-&gt;get('video_id')-&gt;value);
        $storageContent = $query-&gt;execute();
        if ($storageContent) {
          return FALSE;
        }
        return TRUE;
      }
    }

}

And finally in the Entity annotation you need to add the id of your constraint:
 *   constraints = {
 *      &quot;unique_profile_content_constraint&quot; = {}
 *   }
 * )
 */