|
| 1 | +--- |
| 2 | +title: state_type |
| 3 | +--- |
| 4 | + |
| 5 | +# State and Error Handling Derived Type |
| 6 | + |
| 7 | +[TOC] |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +The `stdlib_error` module provides a derived type holding information on the state of operations within the standard library and procedures for expert control of workflows. |
| 12 | +An optional `state_type` variable to hold such information is provided as a form of expert API. |
| 13 | +If the user does not require state information but fatal errors are encountered during execution, the program will undergo a hard stop. |
| 14 | +Instead, if the state argument is present, the program will never stop but will return detailed error information into the state handler. |
| 15 | + |
| 16 | +## Derived types provided |
| 17 | + |
| 18 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 19 | +### The `state_type` derived type |
| 20 | + |
| 21 | +The `state_type` is defined as a derived type containing an integer error flag and fixed-size character strings to store an error message and the location of the error state change. |
| 22 | +Fixed-size string storage was chosen to facilitate the compiler's memory allocation and ultimately ensure maximum computational performance. |
| 23 | + |
| 24 | +A similarly named generic interface, `state_type`, is provided to allow the developer to create diagnostic messages and raise error flags easily. |
| 25 | +The call starts with an error flag or the location of the event and is followed by an arbitrary list of `integer`, `real`, `complex`, or `character` variables. |
| 26 | +Numeric variables may be provided as either scalars or rank-1 (array) inputs. |
| 27 | + |
| 28 | +#### Type-bound procedures |
| 29 | + |
| 30 | +The following convenience type-bound procedures are provided: |
| 31 | +- `print()` returns an allocatable character string containing state location, message, and error flag; |
| 32 | +- `print_message()` returns an allocatable character string containing the state message; |
| 33 | +- `ok()` returns a `logical` flag that is `.true.` in case of successful state (`flag==STDLIB_SUCCESS`); |
| 34 | +- `error()` returns a `logical` flag that is `.true.` in case of an error state (`flag/=STDLIB_SUCCESS`). |
| 35 | + |
| 36 | +#### Status |
| 37 | + |
| 38 | +Experimental |
| 39 | + |
| 40 | +#### Example |
| 41 | + |
| 42 | +```fortran |
| 43 | +{!example/error/example_error_state1.f90!} |
| 44 | +``` |
| 45 | + |
| 46 | +## Error flags provided |
| 47 | + |
| 48 | +The module provides the following state flags: |
| 49 | +- `STDLIB_SUCCESS`: Successful execution |
| 50 | +- `STDLIB_VALUE_ERROR`: Numerical errors (such as infinity, not-a-number, range bounds) are encountered. |
| 51 | +- `STDLIB_LINALG_ERROR`: Linear Algebra errors are encountered, such as non-converging iterations, impossible operations, etc. |
| 52 | +- `STDLIB_INTERNAL_ERROR`: Provided as a developer safeguard for internal errors that should never occur. |
| 53 | +- `STDLIB_IO_ERROR`: Input/Output-related errors, such as file reading/writing failures. |
| 54 | +- `STDLIB_FS_ERROR`: File system-related errors, such as directory access issues. |
| 55 | + |
| 56 | +## Comparison operators provided |
| 57 | + |
| 58 | +The module provides overloaded comparison operators for all comparisons of a `state_type` variable with an integer error flag: `<`, `<=`, `==`, `>=`, `>`, `/=`. |
| 59 | + |
0 commit comments