code_to_string Function

public pure function code_to_string(code, user_name) result(name)

Map an error code to a human-readable name

Usage: print *, code_to_string(ERROR_IO) ! “ERROR_IO”

user_name is an optional name for a code PIC does not know about, so that downstream libraries owning codes above PIC_ERROR_CODE_MAX still get readable output: print *, code_to_string(MYLIB_ERROR_SOCKET, “MYLIB_ERROR_SOCKET”) It is ignored for codes PIC recognises, so it can be passed unconditionally. Unknown codes without a user_name still render as “UNKNOWN”.

Arguments

Type IntentOptional Attributes Name
integer(kind=default_int), intent(in) :: code
character(len=*), intent(in), optional :: user_name

Return Value character(len=:), allocatable


Called by

proc~~code_to_string~~CalledByGraph proc~code_to_string code_to_string proc~error_get_full_trace error_t%error_get_full_trace proc~error_get_full_trace->proc~code_to_string proc~error_print_trace error_t%error_print_trace proc~error_print_trace->proc~code_to_string proc~error_fatal error_t%error_fatal proc~error_fatal->proc~error_print_trace

Source Code

   pure function code_to_string(code, user_name) result(name)
      !! Map an error code to a human-readable name
      !!
      !! Usage: print *, code_to_string(ERROR_IO)  ! "ERROR_IO"
      !!
      !! `user_name` is an optional name for a code PIC does not know about,
      !! so that downstream libraries owning codes above PIC_ERROR_CODE_MAX
      !! still get readable output:
      !!   print *, code_to_string(MYLIB_ERROR_SOCKET, "MYLIB_ERROR_SOCKET")
      !! It is ignored for codes PIC recognises, so it can be passed
      !! unconditionally. Unknown codes without a `user_name` still render as
      !! "UNKNOWN".
      integer(default_int), intent(in) :: code
      character(len=*), intent(in), optional :: user_name
      character(len=:), allocatable :: name

      select case (code)
      case (SUCCESS)
         name = "SUCCESS"
      case (ERROR_GENERIC)
         name = "ERROR_GENERIC"
      case (ERROR_IO)
         name = "ERROR_IO"
      case (ERROR_PARSE)
         name = "ERROR_PARSE"
      case (ERROR_VALIDATION)
         name = "ERROR_VALIDATION"
      case (ERROR_ALLOC)
         name = "ERROR_ALLOC"
      case (ERROR_INTERNAL)
         name = "ERROR_INTERNAL"
      case (ERROR_BOUNDS)
         name = "ERROR_BOUNDS"
      case default
         if (present(user_name)) then
            name = trim(user_name)
         else
            name = "UNKNOWN"
         end if
      end select
   end function code_to_string