module simple_error_handler
  use mpi
  implicit none

  public

  contains

  !--------------------------------------------------------------------------!
  subroutine error_check(ierr, err_str, fatal)
    implicit none

    integer,            intent(in)            :: ierr
    character(len=*),   intent(in)            :: err_str
    logical,            intent(in), optional  :: fatal
    character(32)                             :: err_prep
    logical                                   :: o_fatal
    integer                                   :: oerr

    o_fatal = .true.
    if (present(fatal)) o_fatal = fatal


    err_prep = "ERROR: "
    if (ierr .ne. 0) then
      print *, trim(err_prep) // trim (err_str)
      if (o_fatal) call mpi_abort(mpi_comm_world, ierr, oerr)
    endif
  end subroutine error_check
end module simple_error_handler
