Missing field - how to check if field exists in entity

If you are getting something like

Drupal\Core\Entity\EntityStorageException: Field user_source is unknown. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 846 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.p

you are missing a field in some entity, proper way to have a block of code would be to first check if this field is present in that entity, so you would use

    if ($this->hasField('user_source')) {


    if ($entity->hasField('user_source')) {

so if you later have this line of code that was breaking a site it wouldnt have got to it and broke site.

 $user_source = $this->get('user_source')->value;