-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_v.f90
More file actions
111 lines (78 loc) · 3.01 KB
/
error_v.f90
File metadata and controls
111 lines (78 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
!> Error value
!>
!> Inspired by the excellent, MIT licensed
!> https://github.com/samharrison7/fortran-error-handler
!>
!> Fortran doesn't have a null value.
!> As a result, we introduce this derived type
!> with the convention that a code of `NO_ERROR_CODE` (0)
!> indicates no error (i.e. is our equivalent of a null value).
module m_error_v
implicit none
private
integer, parameter, public :: NO_ERROR_CODE = 0
!! Code that indicates no error
type, public :: ErrorV
!! Error value
integer :: code = 1
!! Error code
character(len=512) :: message = ""
!! Error message
! TODO: think about making the message allocatable to handle long messages
! TODO: think about adding idea of critical
! (means you can stop but also unwind errors and traceback along the way)
! TODO: think about adding trace (might be simpler than compiling with traceback)
! type(ErrorV), allocatable, dimension(:) :: causes
contains
private
procedure, public :: build
procedure, public :: finalise
final :: finalise_auto
! get_res sort of not needed (?)
! get_err sort of not needed (?)
end type ErrorV
interface ErrorV
!! Constructor interface - see build (TODO: figure out cross-ref syntax) for details
module procedure :: constructor
end interface ErrorV
contains
function constructor(code, message) result(self)
!! Constructor - see build (TODO: figure out cross-ref syntax) for details
integer, intent(in) :: code
character(len=*), optional, intent(in) :: message
type(ErrorV) :: self
call self % build(code, message)
end function constructor
subroutine build(self, code, message)
!! Build instance
class(ErrorV), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
integer, intent(in) :: code
!! Error code
!!
!! Use [TODO: figure out xref] `NO_ERROR_CODE` if there is no error
character(len=*), optional, intent(in) :: message
!! Error message
self % code = code
if (present(message)) then
self % message = message
end if
end subroutine build
subroutine finalise(self)
!! Finalise the instance (i.e. free/deallocate)
class(ErrorV), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
! If we make message allocatable, deallocate here
self % code = 1
self % message = ""
end subroutine finalise
subroutine finalise_auto(self)
!! Finalise the instance (i.e. free/deallocate)
!!
!! This method is expected to be called automatically
!! by clever clean up, which is why it differs from [TODO x-ref] `finalise`
type(ErrorV), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
call self % finalise()
end subroutine finalise_auto
end module m_error_v