error_t Derived Type

type, public :: error_t


Components

Type Visibility Attributes Name Initial
character(len=MAX_LOCATION_LEN), public :: call_stack(MAX_STACK_DEPTH)

Call locations

Cause chain support (Rust-style “caused by”)

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

Stack trace support

integer(kind=default_int), public :: stack_depth = 0

Current stack depth


Type-Bound Procedures

procedure, public :: add_context => error_add_context

  • private pure subroutine error_add_context(self, location)

    Add a call location to the stack trace Typically called when propagating errors upward

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(inout) :: self
    character(len=*), intent(in) :: location

procedure, public :: clear => error_clear

  • private pure subroutine error_clear(self)

    Clear the error state, stack trace, and cause chain

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(inout) :: self

procedure, public :: fatal => error_fatal

  • private subroutine error_fatal(self, unit)

    Print the error trace and stop the program Use for unrecoverable errors

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self
    integer(kind=default_int), intent(in), optional :: unit

procedure, public :: get_code => error_get_code

  • private pure function error_get_code(self) result(code)

    Get the error code

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self

    Return Value integer(kind=default_int)

procedure, public :: get_full_trace => error_get_full_trace

  • private function error_get_full_trace(self) result(trace)

    Get complete error message with cause chain and stack trace Returns a dynamically-sized multi-line string

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self

    Return Value character(len=:), allocatable

procedure, public :: get_message => error_get_message

  • private pure function error_get_message(self) result(message)

    Get the error message (without stack trace)

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self

    Return Value character(len=:), allocatable

procedure, public :: has_error => error_has_error

  • private pure function error_has_error(self) result(has_err)

    Check if an error is set

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self

    Return Value logical

procedure, public :: is => error_is

  • private pure function error_is(self, code) result(matches)

    Check if the error matches a specific error code

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self
    integer(kind=default_int), intent(in) :: code

    Return Value logical

procedure, public :: print_trace => error_print_trace

  • private subroutine error_print_trace(self, unit)

    Print error with cause chain and stack trace to specified unit If unit not specified, prints to stdout

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: self
    integer(kind=default_int), intent(in), optional :: unit

procedure, public :: set => error_set

  • private pure subroutine error_set(self, code, message)

    Set an error with code and message Resets the stack trace and cause chain

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(inout) :: self
    integer(kind=default_int), intent(in) :: code
    character(len=*), intent(in) :: message

procedure, public :: wrap => error_wrap

  • private pure subroutine error_wrap(self, code, 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”)

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(inout) :: self
    integer(kind=default_int), intent(in) :: code
    character(len=*), intent(in) :: message

Source Code

   type :: error_t
      integer(default_int) :: code = SUCCESS
         !! Error code (0 = no error)
      character(len=:), allocatable :: message
         !! Error message

      !! Stack trace support
      integer(default_int) :: stack_depth = 0
         !! Current stack depth
      character(len=MAX_LOCATION_LEN) :: call_stack(MAX_STACK_DEPTH)
         !! Call locations

      !! Cause chain support (Rust-style "caused by")
      integer(default_int) :: cause_depth = 0
         !! Number of wrapped causes
      integer(default_int) :: cause_codes(MAX_CAUSE_DEPTH)
         !! Error codes of wrapped causes
      character(len=MAX_CAUSE_MSG_LEN) :: cause_messages(MAX_CAUSE_DEPTH)
         !! Messages of wrapped causes
   contains
      procedure :: has_error => error_has_error
      procedure :: set => error_set
      procedure :: clear => error_clear
      procedure :: get_code => error_get_code
      procedure :: get_message => error_get_message
      procedure :: is => error_is
      procedure :: wrap => error_wrap
      procedure :: add_context => error_add_context
      procedure :: get_full_trace => error_get_full_trace
      procedure :: print_trace => error_print_trace
      procedure :: fatal => error_fatal
   end type error_t