Can the errormessage of pcall be nil when success is false?

Given the following code (taken from MarketplaceService.ProcessReceipt):
local success, errorMessage = pcall(function()
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)

Can success be false, while having errorMessage as nil?
Is there an example of this?

If there is no error, ‘success’ is true and error message is nil.
If there is an error, success is false and error message is not nil.

AFAIK, no it can’t.

EDIT:

The pcall function calls its first argument in protected mode , so that it catches any errors while the function is running. If there are no errors, pcall returns true , plus any values returned by the call. Otherwise, it returns false , plus the error message.

This was taken from the official Lua website:

https://www.lua.org/pil/8.4.html

It could potentially be an empty string, but it should always return something that is a string when there is an error coming from a Roblox API member.

2 Likes

Reading down from the site I linked above, it says this:

Despite its name, the error message does not have to be a string. Any Lua value that you pass to error will be returned by pcall

See @mrow_pizza’s reply below

(Happy cake day!)

Edited above to clarify: “coming from a Roblox API member”. Assuming OP doesn’t call error themselves with something that isn’t a string.

3 Likes

When I was an intern at Roblox working on the home / games / games details pages in Roact, we ran into some cases where pcall returned a nil message. I don’t remember what caused it, but I remember it was fun to debug. If I remember correctly, it wasn’t supposed to ever return nil. In either case, it surprised the Lua Core team that it did. Good luck!

(when error is called with no arguments in a pcall, the message is “An error occurred” i.e print(pcall(error)) --> false An error occurred)

1 Like

This is not true on Roblox; in our Lua environment, pcall’s second return value in the error case is always a string (or nil, possibly). pcall in Roblox will always try to convert the argument to a string, and default to “An error occurred” if it can’t coerce it to one.

2 Likes