jas43 avatar jas43 committed b606636

Refactor error handling in open_config_* routines.

Error handling code is now in its own subroutine rather than copied in
4 different places.

Comments (0)

Files changed (1)

source/aotus_module.f90

     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))
 
     if (.not.flu_isopen(L)) L = fluL_newstate()
 
     err = fluL_loadfile(L, filename)
+    call aot_config_err_handler(L, err, 'Cannot load configuration file:', ErrString, ErrCode)
+
+    call fluL_openlibs(L)
+
+    err = flu_pcall(L, 0, 0, 0)
+    call aot_config_err_handler(L, err, 'Cannot run configuration file:', ErrString, ErrCode)
+
+  end subroutine open_config_file
+
+
+  subroutine close_config(L)
+    type(flu_State) :: L
+
+    call flu_close(L)
+
+  end subroutine close_config
+
+
+  subroutine get_config_real(val, ErrCode, L, key, default)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: key
+    real(kind=single_k), intent(out) :: val
+    integer, intent(out) :: ErrCode
+    real(kind=single_k), optional, intent(in) :: default
+
+    call flu_getglobal(L, key)
+    call aot_top_get_val(val, ErrCode, L, default)
+
+  end subroutine get_config_real
+
+
+  subroutine get_config_double(val, ErrCode, L, key, default)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: key
+    real(kind=double_k), intent(out) :: val
+    integer, intent(out) :: ErrCode
+    real(kind=double_k), optional, intent(in) :: default
+
+    call flu_getglobal(L, key)
+    call aot_top_get_val(val, ErrCode, L, default)
+
+  end subroutine get_config_double
+
+
+  subroutine get_config_integer(val, ErrCode, L, key, default)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: key
+    integer, intent(out) :: val
+    integer, intent(out) :: ErrCode
+    integer, optional, intent(in) :: default
+
+    call flu_getglobal(L, key)
+    call aot_top_get_val(val, ErrCode, L, default)
+
+  end subroutine get_config_integer
+
+
+  subroutine get_config_long(val, ErrCode, L, key, default)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: key
+    integer(kind=long_k), intent(out) :: val
+    integer, intent(out) :: ErrCode
+    integer(kind=long_k), optional, intent(in) :: default
+
+    call flu_getglobal(L, key)
+    call aot_top_get_val(val, ErrCode, L, default)
+
+  end subroutine get_config_long
+
+
+  subroutine get_config_logical(val, ErrCode, L, key, default)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: key
+    logical, intent(out) :: val
+    integer, intent(out) :: ErrCode
+    logical, optional, intent(in) :: default
+
+    call flu_getglobal(L, key)
+    call aot_top_get_val(val, ErrCode, L, default)
+
+  end subroutine get_config_logical
+
+
+  subroutine get_config_string(val, ErrCode, L, key, default)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: key
+    character(len=*) :: val
+    integer, intent(out) :: ErrCode
+    character(len=*), optional, intent(in) :: default
+
+    call flu_getglobal(L, key)
+    call aot_top_get_val(val, ErrCode, L, default)
+
+  end subroutine get_config_string
+
+
+  subroutine open_config_chunk(L, chunk, ErrCode, ErrString)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: chunk
+    integer, intent(out), optional :: ErrCode
+    character(len=*), intent(out), optional :: ErrString
+
+    integer :: err
+
+    if (.not.flu_isopen(L)) L = fluL_newstate()
+
+    err = fluL_loadstring(L, chunk)
+
+    call aot_config_err_handler(L, err, 'Cannot load chunk:', ErrString, ErrCode)
+
+    call fluL_openlibs(L)
+
+    err = flu_pcall(L, 0, 0, 0)
+
+    call aot_config_err_handler(L, err, 'Cannot run chunk:', ErrString, ErrCode)
+
+  end subroutine open_config_chunk
+
+  subroutine aot_config_err_handler(L, err, msg, ErrString, ErrCode)
+
+    type(flu_State) :: L
+    integer, intent(in) :: err
+    character(len=*), intent(in) :: msg
+    character(len=*), intent(out), optional :: ErrString
+    integer, intent(out), optional :: ErrCode
+
+    logical :: stop_on_error
+    character, pointer, dimension(:) :: string
+    integer :: str_len
+    integer :: i
+
+    stop_on_error = .not.(present(ErrString) .or. present(ErrCode))
 
     if (present(ErrCode)) then
       ErrCode = err
       end if
 
       if (stop_on_error) then
-        write(*,*) "cannot load configuration file: ", string
-        STOP
-      else
-        return
-      end if
-
-    end if
-
-    call fluL_openlibs(L)
-
-    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_file
-
-
-  subroutine close_config(L)
-    type(flu_State) :: L
-
-    call flu_close(L)
-
-  end subroutine close_config
-
-
-  subroutine get_config_real(val, ErrCode, L, key, default)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: key
-    real(kind=single_k), intent(out) :: val
-    integer, intent(out) :: ErrCode
-    real(kind=single_k), optional, intent(in) :: default
-
-    call flu_getglobal(L, key)
-    call aot_top_get_val(val, ErrCode, L, default)
-
-  end subroutine get_config_real
-
-
-  subroutine get_config_double(val, ErrCode, L, key, default)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: key
-    real(kind=double_k), intent(out) :: val
-    integer, intent(out) :: ErrCode
-    real(kind=double_k), optional, intent(in) :: default
-
-    call flu_getglobal(L, key)
-    call aot_top_get_val(val, ErrCode, L, default)
-
-  end subroutine get_config_double
-
-
-  subroutine get_config_integer(val, ErrCode, L, key, default)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: key
-    integer, intent(out) :: val
-    integer, intent(out) :: ErrCode
-    integer, optional, intent(in) :: default
-
-    call flu_getglobal(L, key)
-    call aot_top_get_val(val, ErrCode, L, default)
-
-  end subroutine get_config_integer
-
-
-  subroutine get_config_long(val, ErrCode, L, key, default)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: key
-    integer(kind=long_k), intent(out) :: val
-    integer, intent(out) :: ErrCode
-    integer(kind=long_k), optional, intent(in) :: default
-
-    call flu_getglobal(L, key)
-    call aot_top_get_val(val, ErrCode, L, default)
-
-  end subroutine get_config_long
-
-
-  subroutine get_config_logical(val, ErrCode, L, key, default)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: key
-    logical, intent(out) :: val
-    integer, intent(out) :: ErrCode
-    logical, optional, intent(in) :: default
-
-    call flu_getglobal(L, key)
-    call aot_top_get_val(val, ErrCode, L, default)
-
-  end subroutine get_config_logical
-
-
-  subroutine get_config_string(val, ErrCode, L, key, default)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: key
-    character(len=*) :: val
-    integer, intent(out) :: ErrCode
-    character(len=*), optional, intent(in) :: default
-
-    call flu_getglobal(L, key)
-    call aot_top_get_val(val, ErrCode, L, default)
-
-  end subroutine get_config_string
-
-
-  subroutine open_config_chunk(L, chunk, ErrCode, ErrString)
-    type(flu_State) :: L
-    character(len=*), intent(in) :: chunk
-    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))
-
-    if (.not.flu_isopen(L)) L = fluL_newstate()
-
-    err = fluL_loadstring(L, chunk)
-
-    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 chunk: ", string
-        STOP
-      else
-        return
-      end if
-
-    end if
-
-    call fluL_openlibs(L)
-
-    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 chunk: ", string
+        write(*,*) msg, string
         STOP
       end if
 
     end if
 
-  end subroutine open_config_chunk
+  end subroutine aot_config_err_handler
 
 
 end module aotus_module
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.