{"record":{"id":"5960c81ffd05ad93","repo":"puppetlabs/puppet","slug":"formatmessagew-could-not-format-code-code","errorCode":null,"errorMessage":"FormatMessageW could not format code %{code}","messagePattern":"FormatMessageW could not format code %(.+?)","errorType":"exception","errorClass":"Puppet::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/error.rb","lineNumber":45,"sourceCode":"    # 3.User default LANGID, based on the user's default locale value\n    # 4.System default LANGID, based on the system default locale value\n    # 5.US English\n    dwLanguageId = 0\n    flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |\n            FORMAT_MESSAGE_FROM_SYSTEM |\n            FORMAT_MESSAGE_ARGUMENT_ARRAY |\n            FORMAT_MESSAGE_IGNORE_INSERTS |\n            FORMAT_MESSAGE_MAX_WIDTH_MASK\n    error_string = ''.dup\n\n    # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us\n    FFI::MemoryPointer.new(:pointer, 1) do |buffer_ptr|\n      length = FormatMessageW(flags, FFI::Pointer::NULL, code, dwLanguageId,\n                              buffer_ptr, 0, FFI::Pointer::NULL)\n\n      if length == FFI::WIN32_FALSE\n        # can't raise same error type here or potentially recurse infinitely\n        raise Puppet::Error, _(\"FormatMessageW could not format code %{code}\") % { code: code }\n      end\n\n      # returns an FFI::Pointer with autorelease set to false, which is what we want\n      buffer_ptr.read_win32_local_pointer do |wide_string_ptr|\n        if wide_string_ptr.null?\n          raise Puppet::Error, _(\"FormatMessageW failed to allocate buffer for code %{code}\") % { code: code }\n        end\n\n        error_string = wide_string_ptr.read_wide_string(length)\n      end\n    end\n\n    error_string\n  end\n\n  ERROR_FILE_NOT_FOUND      = 2\n  ERROR_ACCESS_DENIED       = 5\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/error.rb#L27-L63","documentation":"Puppet::Util::Windows::Error.format_error_code raises this plain Puppet::Error when the Win32 FormatMessageW call itself fails while rendering a Windows error code to text — typically because the code is not a system message id or the message table/language resource is unavailable. The raise deliberately uses Puppet::Error (not Windows::Error) to avoid infinite recursion when formatting the failure of formatting.","triggerScenarios":"Constructing Puppet::Util::Windows::Error.new(msg, code) — which always calls format_error_code — with a bogus or non-Win32 code (e.g. an FFI wrapper returning a custom HRESULT from a message table Puppet cannot reach), during error handling on Windows.","commonSituations":"Passing arbitrary integer return codes from FFI bindings into Puppet's error class; secondary noise that obscures the original failure being reported.","solutions":["Validate that the code is a genuine Win32/HRESULT value before formatting; otherwise print it numerically.","Report upstream if a legitimate system code fails to format — the message table may be missing on that install."],"exampleFix":"# before\nraise Puppet::Util::Windows::Error.new('request failed', arbitrary_code)\n\n# after - keep a numeric fallback for codes you don't control\nbegin\n  raise Puppet::Util::Windows::Error.new('request failed', arbitrary_code)\nrescue Puppet::Error\n  raise \"request failed (Windows code 0x#{arbitrary_code.to_s(16).upcase})\"\nend","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"def safe_win32_message(code)\n  Puppet::Util::Windows::Error.format_error_code(code)\nrescue Puppet::Error\n  \"unformatted Windows error 0x#{code.to_s(16).upcase}\"\nend","preventionTips":["Never pass arbitrary integers to Windows::Error without a rescue.","Keep a numeric-code fallback beside every FFI return-code check.","Log both the code and the formatted text when both are available."],"tags":["puppet","windows","win32","formatmessage","ffi","error-handling"],"backgroundTag":"win32-error-formatting-failed","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}