|
| 1 | +!> Error value |
| 2 | +!> |
| 3 | +!> Inspired by the excellent, MIT licensed |
| 4 | +!> https://github.com/samharrison7/fortran-error-handler |
| 5 | +!> |
| 6 | +!> Fortran doesn't have a null value. |
| 7 | +!> As a result, we introduce this derived type |
| 8 | +!> with the convention that a code of 0 indicates no error. |
| 9 | +module m_error_v |
| 10 | + |
| 11 | + ! TODO: switch `m_` prefix to project name prefix |
| 12 | + ! (e.g. fpyfgen, "Fortran-code for Python-Fortran wrapper Generator") |
| 13 | + ! (same idea as `tomlf_` prefix) |
| 14 | + ! TODO: move this to standalone repo (eventually) |
| 15 | + use fpyfgen_base_finalisable, only: BaseFinalisable |
| 16 | + |
| 17 | + implicit none |
| 18 | + private |
| 19 | + |
| 20 | + integer, parameter, public :: NO_ERROR_CODE = 0 |
| 21 | + !! Code that indicates no error |
| 22 | + |
| 23 | + type, extends(BaseFinalisable), public :: ErrorV |
| 24 | + !! Error value |
| 25 | + |
| 26 | + integer :: code = 1 |
| 27 | + !! Error code |
| 28 | + |
| 29 | + character(len=128) :: message = "" |
| 30 | + ! TODO: think about making the message allocatable to handle long messages |
| 31 | + |
| 32 | + ! TODO: think about adding idea of critical |
| 33 | + ! (means you can stop but also unwind errors and traceback along the way) |
| 34 | + |
| 35 | + ! TODO: think about adding trace (might be simpler than compiling with traceback) |
| 36 | + |
| 37 | + contains |
| 38 | + |
| 39 | + private |
| 40 | + |
| 41 | + procedure, public:: build, finalise |
| 42 | + ! get_res sort of not needed (?) |
| 43 | + ! get_err sort of not needed (?) |
| 44 | + |
| 45 | + end type ErrorV |
| 46 | + |
| 47 | + interface ErrorV |
| 48 | + !! Constructor interface - see build (TODO: figure out cross-ref syntax) for details |
| 49 | + ! This is what allows us to do ErrorV(...) when using this class |
| 50 | + module procedure :: constructor |
| 51 | + end interface ErrorV |
| 52 | + |
| 53 | +contains |
| 54 | + |
| 55 | + function constructor(code, message) result(self) |
| 56 | + !! Constructor - see build (TODO: figure out cross-ref syntax) for details |
| 57 | + |
| 58 | + integer, intent(in) :: code |
| 59 | + character(len=*), optional, intent(in) :: message |
| 60 | + |
| 61 | + type(ErrorV) :: self |
| 62 | + |
| 63 | + call self % build(code, message) |
| 64 | + |
| 65 | + end function constructor |
| 66 | + |
| 67 | + subroutine build(self, code, message) |
| 68 | + !! Build instance |
| 69 | + |
| 70 | + class(ErrorV), intent(inout) :: self |
| 71 | + ! Hopefully can leave without docstring (like Python) |
| 72 | + |
| 73 | + integer, intent(in) :: code |
| 74 | + !! Error code |
| 75 | + !! |
| 76 | + !! Use [TODO: figure out xref] `NO_ERROR_CODE` if there is no error |
| 77 | + |
| 78 | + character(len=*), optional, intent(in) :: message |
| 79 | + !! Error message |
| 80 | + |
| 81 | + self % code = code |
| 82 | + if (present(message)) then |
| 83 | + self % message = message |
| 84 | + end if |
| 85 | + |
| 86 | + end subroutine build |
| 87 | + |
| 88 | + subroutine finalise(self) |
| 89 | + !! Finalise the instance (i.e. free/deallocate) |
| 90 | + |
| 91 | + class(ErrorV), intent(inout) :: self |
| 92 | + ! Hopefully can leave without docstring (like Python) |
| 93 | + |
| 94 | + ! If we make message allocatable, deallocate here |
| 95 | + |
| 96 | + end subroutine finalise |
| 97 | + |
| 98 | +end module m_error_v |
0 commit comments