error_wrap Subroutine

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”)

Print order: outermost wrapper first, root cause last (like Rust).

Usage: call parse_json(data, err) if (err%has_error()) then call err%wrap(ERROR_PARSE, “failed to load config file”) return end if

Produces: ERROR_PARSE: failed to load config file Caused by: ERROR_IO: could not read file “input.json”

Type Bound

error_t

Arguments

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

Calls

proc~~error_wrap~~CallsGraph proc~error_wrap error_t%error_wrap proc~error_has_error error_t%error_has_error proc~error_wrap->proc~error_has_error

Source Code

   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")
      !!
      !! Print order: outermost wrapper first, root cause last (like Rust).
      !!
      !! Usage:
      !!   call parse_json(data, err)
      !!   if (err%has_error()) then
      !!      call err%wrap(ERROR_PARSE, "failed to load config file")
      !!      return
      !!   end if
      !!
      !! Produces:
      !!   ERROR_PARSE: failed to load config file
      !!     Caused by: ERROR_IO: could not read file "input.json"
      class(error_t), intent(inout) :: self
      integer(default_int), intent(in) :: code
      character(len=*), intent(in) :: message

      if (.not. self%has_error()) return

      ! Push current error into cause chain
      if (self%cause_depth < MAX_CAUSE_DEPTH) then
         self%cause_depth = self%cause_depth + 1
         self%cause_codes(self%cause_depth) = self%code
         if (allocated(self%message)) then
            self%cause_messages(self%cause_depth) = self%message
         else
            self%cause_messages(self%cause_depth) = "(no message)"
         end if
      end if

      ! Set new top-level error
      self%code = code
      self%message = trim(message)
   end subroutine error_wrap