error_t Derived Type

type, public :: error_t


Inherited by

type~~error_t~~InheritedByGraph type~error_t error_t type~calculation_result_t calculation_result_t type~calculation_result_t->type~error_t error

Components

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

Call locations

integer, public :: code = SUCCESS

Error code (0 = no error)

character(len=:), public, allocatable :: message

Error message

Stack trace support

integer, public :: stack_depth = 0

Current stack depth


Type-Bound Procedures

procedure, public :: add_context => error_add_context

  • private pure subroutine error_add_context(this, 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) :: this
    character(len=*), intent(in) :: location

procedure, public :: clear => error_clear

  • private pure subroutine error_clear(this)

    Clear the error state and stack trace

    Arguments

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

procedure, public :: get_code => error_get_code

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

    Get the error code

    Arguments

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

    Return Value integer

procedure, public :: get_full_trace => error_get_full_trace

  • private function error_get_full_trace(this) result(trace)

    Get complete error message with stack trace Returns a multi-line string with error and call stack

    Arguments

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

    Return Value character(len=:), allocatable

procedure, public :: get_message => error_get_message

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

    Get the error message (without stack trace)

    Arguments

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

    Return Value character(len=:), allocatable

procedure, public :: has_error => error_has_error

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

    Check if an error is set

    Arguments

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

    Return Value logical

procedure, public :: print_trace => error_print_trace

  • private subroutine error_print_trace(this, unit)

    Print error with stack trace to specified unit If unit not specified, prints to stdout (unit 6)

    Arguments

    Type IntentOptional Attributes Name
    class(error_t), intent(in) :: this
    integer, intent(in), optional :: unit

procedure, public :: set => error_set

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

    Set an error with code and message Resets the stack trace

    Arguments

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

Source Code

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

      !! Stack trace support
      integer :: stack_depth = 0  !! Current stack depth
      character(len=MAX_LOCATION_LEN) :: call_stack(MAX_STACK_DEPTH)  !! Call locations
   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 :: add_context => error_add_context
      procedure :: get_full_trace => error_get_full_trace
      procedure :: print_trace => error_print_trace
   end type error_t