Unified error handling with stack traces for PIC. Use error_t as an intent(out) or intent(inout) argument to propagate errors with context through call chains.
Basic usage: type(error_t) :: err call err%set(ERROR_IO, “failed to open file”) if (err%has_error()) call err%fatal() ! Or using operator: if (.haserror. err) call err%fatal()
Error wrapping (Rust-style “caused by”, outermost first): call low_level_routine(err) if (err%has_error()) then call err%wrap(ERROR_PARSE, “failed to parse input”) return end if Error codes Stack trace configuration
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=default_int), | public, | parameter | :: | ERROR_ALLOC | = | 5 | |
| integer(kind=default_int), | public, | parameter | :: | ERROR_GENERIC | = | 1 | |
| integer(kind=default_int), | public, | parameter | :: | ERROR_IO | = | 2 | |
| integer(kind=default_int), | public, | parameter | :: | ERROR_PARSE | = | 3 | |
| integer(kind=default_int), | public, | parameter | :: | ERROR_VALIDATION | = | 4 | |
| integer(kind=default_int), | public, | parameter | :: | SUCCESS | = | 0 | |
| integer(kind=default_int), | private, | parameter | :: | MAX_CAUSE_DEPTH | = | 8 | |
| integer(kind=default_int), | private, | parameter | :: | MAX_CAUSE_MSG_LEN | = | 256 |
Unified error type with stack trace support |
| integer(kind=default_int), | private, | parameter | :: | MAX_LOCATION_LEN | = | 128 |
Cause chain configuration |
| integer(kind=default_int), | private, | parameter | :: | MAX_STACK_DEPTH | = | 20 |
Operator for checking error state: if (.haserror. err) then
Check if an error is set
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=MAX_LOCATION_LEN), | public | :: | call_stack(MAX_STACK_DEPTH) |
Call locations |
|||
| integer(kind=default_int), | public | :: | cause_codes(MAX_CAUSE_DEPTH) |
Error codes of wrapped causes |
|||
| integer(kind=default_int), | public | :: | cause_depth | = | 0 |
Number of wrapped causes |
|
| character(len=MAX_CAUSE_MSG_LEN), | public | :: | cause_messages(MAX_CAUSE_DEPTH) |
Messages of wrapped causes |
|||
| integer(kind=default_int), | public | :: | code | = | SUCCESS |
Error code (0 = no error) |
|
| character(len=:), | public, | allocatable | :: | message |
Error message |
||
| integer(kind=default_int), | public | :: | stack_depth | = | 0 |
Current stack depth |
| procedure, public :: add_context => error_add_context | |
| procedure, public :: clear => error_clear | |
| procedure, public :: fatal => error_fatal | |
| procedure, public :: get_code => error_get_code | |
| procedure, public :: get_full_trace => error_get_full_trace | |
| procedure, public :: get_message => error_get_message | |
| procedure, public :: has_error => error_has_error | |
| procedure, public :: is => error_is | |
| procedure, public :: print_trace => error_print_trace | |
| procedure, public :: set => error_set | |
| procedure, public :: wrap => error_wrap |
Map an error code to a human-readable name
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=default_int), | intent(in) | :: | code |
Get the error code
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self |
Get complete error message with cause chain and stack trace Returns a dynamically-sized multi-line string
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self |
Get the error message (without stack trace)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self |
Check if an error is set
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self |
Check if the error matches a specific error code
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self | |||
| integer(kind=default_int), | intent(in) | :: | code |
Add a call location to the stack trace Typically called when propagating errors upward
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(inout) | :: | self | |||
| character(len=*), | intent(in) | :: | location |
Clear the error state, stack trace, and cause chain
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(inout) | :: | self |
Print the error trace and stop the program Use for unrecoverable errors
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self | |||
| integer(kind=default_int), | intent(in), | optional | :: | unit |
Print error with cause chain and stack trace to specified unit If unit not specified, prints to stdout
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(in) | :: | self | |||
| integer(kind=default_int), | intent(in), | optional | :: | unit |
Set an error with code and message Resets the stack trace and cause chain
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(inout) | :: | self | |||
| integer(kind=default_int), | intent(in) | :: | code | |||
| character(len=*), | intent(in) | :: | message |
Wrap the current error with a higher-level context Pushes the current error into the cause chain and sets a new top-level code and message (Rust-style “caused by”)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(error_t), | intent(inout) | :: | self | |||
| integer(kind=default_int), | intent(in) | :: | code | |||
| character(len=*), | intent(in) | :: | message |