code: def check_layer(layer, has_geometry=True):
      
      if is_vector_layer(layer) or is_raster_layer(layer):
          if not layer.isValid():
              raise InvalidLayerError(
                  'The layer is invalid : %s' % layer.publicSource())
  
          if is_vector_layer(layer):
  
              sub_layers = layer.dataProvider().subLayers()
              if len(sub_layers) > 1:
                  names = ';'.join(sub_layers)
                  source = layer.source()
                  raise InvalidLayerError(
                      tr('The layer should not have many sublayers : {source} : '
                         '{names}').format(source=source, names=names))
  
              # We only check the geometry if we have at least one feature.
  
              if layer.geometryType() == QgsWkbTypes.UnknownGeometry and (
                      layer.featureCount() != 0):
                  raise InvalidLayerError(
                      tr('The layer has not a valid geometry type.'))
  
              if layer.wkbType() == QgsWkbTypes.Unknown and (
                      layer.featureCount() != 0):
                  raise InvalidLayerError(
                      tr('The layer has not a valid geometry type.'))
  
              if isinstance(has_geometry, bool) and layer.featureCount() != 0:
                  if layer.isSpatial() != has_geometry:
                      raise InvalidLayerError(
                          tr('The layer has not a correct geometry type.'))
  
      else:
          raise InvalidLayerError(
              tr('The layer is neither a raster nor a vector : {type}').format(
                  type=type(layer)))
  
      return True
docs: Helper to check layer validity.
  
      This function wil; raise InvalidLayerError if the layer is invalid.
  
      :param layer: The layer to check.
      :type layer: QgsMapLayer
  
      :param has_geometry: If the layer must have a geometry. True by default.
          If it's a raster layer, we will no check this parameter. If we do not
          want to check the geometry type, we can set it to None.
      :type has_geometry: bool,None
  
      :raise: InvalidLayerError
  
      :return: Return True if the layer is valid.
      :rtype: bool