Commits

Harald Klimach committed 149cc7b

Do not write to standard output and abort.
Instead return an error code and leave the handling to the caller. Standard
output is especially annoying in parallel runs and error handling by the caller
is generally more flexible.
Only if no parameters for the error handling are provided, keep the old
behavior.

Comments (0)

Files changed (4)

source/aotus_module.f90

 
 contains
 
-  subroutine open_config(L, filename)
+  subroutine open_config(L, filename, ErrCode, ErrString)
     type(flu_State) :: L
     character(len=*), intent(in) :: filename
+    integer, intent(out), optional :: ErrCode
+    character(len=*), intent(out), optional :: ErrString
+
+    character, pointer, dimension(:) :: string
     integer :: str_len
+    integer :: err
+    integer :: i
+    logical :: stop_on_error
+
+    stop_on_error = .not.(present(ErrString) .or. present(ErrCode))
 
     L = fluL_newstate()
 
-    if (fluL_loadfile(L, filename) .ne. 0) then
-      write(*,*) "cannot load configuration file: ", &
-        &        flu_tolstring(L, -1, str_len)
-      STOP
+    err = fluL_loadfile(L, filename)
+
+    if (present(ErrCode)) then
+      ErrCode = err
+    end if
+
+    if (err .ne. 0) then
+
+      string => flu_tolstring(L, -1, str_len)
+      if (present(ErrString)) then
+        do i=1,min(str_len, len(ErrString))
+          ErrString(i:i) = string(i)
+        end do
+      end if
+
+      if (stop_on_error) then
+        write(*,*) "cannot load configuration file: ", string
+        STOP
+      else
+        return
+      end if
+
     end if
 
     call fluL_openlibs(L)
 
-    if (flu_pcall(L, 0, 0, 0) .ne. 0) then
-      write(*,*) "cannot run configuration file: ", &
-        &        flu_tolstring(L, -1, str_len)
-      STOP
+    err = flu_pcall(L, 0, 0, 0)
+
+    if (present(ErrCode)) then
+      ErrCode = err
+    end if
+
+    if (err .ne. 0) then
+
+      string => flu_tolstring(L, -1, str_len)
+      if (present(ErrString)) then
+        do i=1,min(str_len, len(ErrString))
+          ErrString(i:i) = string(i)
+        end do
+      end if
+
+      if (stop_on_error) then
+        write(*,*) "cannot run configuration file: ", string
+        STOP
+      else
+        return
+      end if
+
     end if
 
   end subroutine open_config

test/aotus_test.f90

   real(kind=double_k) :: coord(3)
   real(kind=double_k) :: results
 
-  call open_config(L = conf, filename = 'config.lua')
+  call open_config(L = conf, filename = 'config.lua', ErrCode = iError)
+  if (iError /= 0) then
+    write(*,*) 'FATAL Error when opening the Lua config file'
+    STOP
+  end if
 
   call aot_get_val(L = conf, key = 'width', &
     &              val = width, ErrCode = iError)

utests/aot_table_test.f90

   integer :: tablen
   integer :: iError
   integer :: i
+  character(len=80) :: ErrString
 
   call create_script('aot_table_test_config.lua')
+
   write(*,*)
   write(*,*) 'Running aot_table_test...'
-  call open_config(L = conf, filename = 'aot_table_test_config.lua')
+
+  call open_config(L = conf, filename = 'aot_table_test_config.lua', &
+    &              ErrCode = iError, ErrString = ErrString)
+  if (iError /= 0) then
+    write(*,*) 'Unexpected FATAL Error occured !!!'
+    write(*,*) 'Could not open the config file aot_table_test_config.lua:'
+    write(*,*) trim(ErrString)
+    STOP
+  end if
 
   ! Testing for global Table
   write(*,*) ' * opening a global table'

utests/aotus_test.f90

   real :: glob_real
   logical :: glob_log
   character(len=20) :: glob_string
+  character(len=80) :: ErrString
 
   call create_script('aotus_test_config.lua')
   write(*,*)
   write(*,*) 'Running aotus_test...'
   write(*,*) ' * open_config (aotus_test_config.lua)'
-  call open_config(L = conf, filename = 'aotus_test_config.lua')
+  call open_config(L = conf, filename = 'aotus_test_config.lua', &
+    &              ErrCode = iError, ErrString = ErrString)
+  if (iError /= 0) then
+    write(*,*) ' : unexpected FATAL Error occured !!!'
+    write(*,*) ' : Could not open the config file aotus_test_config.lua:'
+    write(*,*) trim(ErrString)
+    STOP
+  end if
   write(*,*) '  : success.'
 
   ! Testing for global INTEGER