Skip to content

Commit dee4b1d

Browse files
Merge pull request #320 from jacobwilliams/error-stop
Error stop option
2 parents e6ab0ac + ec1e371 commit dee4b1d

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ coverage:
99
status:
1010
patch:
1111
default:
12-
target: 70%
12+
target: 50%
1313
project:
1414
default:
1515
target: 70%

src/json_file_module.F90

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
371371
path_separator,&
372372
compress_vectors,&
373373
allow_duplicate_keys,&
374-
escape_solidus)
374+
escape_solidus,&
375+
stop_on_error)
375376

376377
implicit none
377378

@@ -390,7 +391,8 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
390391
path_separator,&
391392
compress_vectors,&
392393
allow_duplicate_keys,&
393-
escape_solidus)
394+
escape_solidus,&
395+
stop_on_error)
394396

395397
end subroutine initialize_json_core_in_file
396398
!*****************************************************************************************
@@ -456,7 +458,8 @@ function initialize_json_file(p,verbose,compact_reals,&
456458
path_separator,&
457459
compress_vectors,&
458460
allow_duplicate_keys,&
459-
escape_solidus) result(file_object)
461+
escape_solidus,&
462+
stop_on_error) result(file_object)
460463

461464
implicit none
462465

@@ -477,7 +480,8 @@ function initialize_json_file(p,verbose,compact_reals,&
477480
path_separator,&
478481
compress_vectors,&
479482
allow_duplicate_keys,&
480-
escape_solidus)
483+
escape_solidus,&
484+
stop_on_error)
481485

482486
if (present(p)) file_object%p => p
483487

src/json_initialize_arguments.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ logical(LK),intent(in),optional :: escape_solidus
7373
!! Note that this option does not affect parsing
7474
!! (both escaped and unescaped are still valid in
7575
!! all cases).
76+
logical(LK),intent(in),optional :: stop_on_error
77+
!! If an exception is raised, then immediately quit.
78+
!! (Default is False).
7679

src/json_value_module.F90

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ module json_value_module
167167

168168
logical(LK) :: is_verbose = .false. !! if true, all exceptions are
169169
!! immediately printed to console.
170+
171+
logical(LK) :: stop_on_error = .false. !! if true, then the program is
172+
!! stopped immediately when an
173+
!! exception is raised.
174+
170175
logical(LK) :: exception_thrown = .false. !! The error flag. Will be set to true
171176
!! when an error is thrown in the class.
172177
!! Many of the methods will check this
@@ -814,7 +819,8 @@ function initialize_json_core(verbose,compact_reals,&
814819
path_separator,&
815820
compress_vectors,&
816821
allow_duplicate_keys,&
817-
escape_solidus) result(json_core_object)
822+
escape_solidus,&
823+
stop_on_error) result(json_core_object)
818824

819825
implicit none
820826

@@ -833,7 +839,8 @@ function initialize_json_core(verbose,compact_reals,&
833839
path_separator,&
834840
compress_vectors,&
835841
allow_duplicate_keys,&
836-
escape_solidus)
842+
escape_solidus,&
843+
stop_on_error)
837844

838845
end function initialize_json_core
839846
!*****************************************************************************************
@@ -869,7 +876,8 @@ subroutine json_initialize(me,verbose,compact_reals,&
869876
path_separator,&
870877
compress_vectors,&
871878
allow_duplicate_keys,&
872-
escape_solidus)
879+
escape_solidus,&
880+
stop_on_error)
873881

874882
implicit none
875883

@@ -904,6 +912,8 @@ subroutine json_initialize(me,verbose,compact_reals,&
904912
!various optional inputs:
905913
if (present(spaces_per_tab)) &
906914
me%spaces_per_tab = spaces_per_tab
915+
if (present(stop_on_error)) &
916+
me%stop_on_error = stop_on_error
907917
if (present(verbose)) &
908918
me%is_verbose = verbose
909919
if (present(strict_type_checking)) &
@@ -1789,6 +1799,8 @@ end subroutine json_clear_exceptions
17891799
!
17901800
!@note If `is_verbose` is true, this will also print a
17911801
! traceback if the Intel compiler is used.
1802+
!
1803+
!@note If `stop_on_error` is true, then the program is stopped.
17921804

17931805
subroutine json_throw_exception(json,msg)
17941806

@@ -1804,14 +1816,31 @@ subroutine json_throw_exception(json,msg)
18041816
json%exception_thrown = .true.
18051817
json%err_message = trim(msg)
18061818

1807-
if (json%is_verbose) then
1819+
if (json%stop_on_error) then
1820+
1821+
#ifdef __INTEL_COMPILER
1822+
! for Intel, we raise a traceback and quit
1823+
call tracebackqq(string=trim(msg), user_exit_code=0)
1824+
#else
1825+
write(error_unit,'(A)') 'JSON-Fortran Exception: '//trim(msg)
1826+
error stop 1
1827+
#endif
1828+
1829+
elseif (json%is_verbose) then
1830+
18081831
write(output_unit,'(A)') '***********************'
18091832
write(output_unit,'(A)') 'JSON-Fortran Exception: '//trim(msg)
1810-
!call backtrace() ! gfortran (use -fbacktrace -fall-intrinsics flags)
1833+
1834+
!#if defined __GFORTRAN__
1835+
! call backtrace() ! (have to compile with -fbacktrace -fall-intrinsics flags)
1836+
!#endif
1837+
18111838
#ifdef __INTEL_COMPILER
18121839
call tracebackqq(user_exit_code=-1) ! print a traceback and return
18131840
#endif
1841+
18141842
write(output_unit,'(A)') '***********************'
1843+
18151844
end if
18161845

18171846
end subroutine json_throw_exception

src/tests/jf_test_30.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ subroutine test_30(error_cnt)
3939
write(error_unit,'(A)') 'escape_solidus = '//trim(tf(i))
4040
write(error_unit,'(A)') ''
4141

42-
call json%initialize(escape_solidus=(i==1))
42+
call json%initialize(escape_solidus=(i==1), stop_on_error=.true.)
4343
call json%load_from_string(str)
4444
call json%print_file()
4545

0 commit comments

Comments
 (0)