Thrown error() disregards pcall due to yielding

Server.Functions.ViewBanReason.OnServerInvoke = function(mod, playerName)
		if self:IsMod(mod) then
			local response = ""
			
			local success, msg = pcall(function()
				local playerId = Server.Players:GetUserIdFromNameAsync(playerName)
				local banInfo = self.BanLogsStore:GetAsync(playerId)
				
				if playerId and banInfo then
					local bannedByName = Server.Players:GetNameFromUserIdAsync(banInfo.BannedBy)
					response = bannedByName .. ": \"" .. banInfo.Reason .. "\""
				else
					error("No player/logs found!")
				end
			end)
			
			if success then
				return response
			else
				return "Error: " .. msg
			end
		end
end

This code is meant to catch the error() call and return it back to the client. What happens instead is the pcall is completely disregarded and an error is thrown to the output with a blue tab next to it “I believe that’s the client”. This could be because it’s in a remotefunction invocation handler.

1 Like

More specifically, this issue is caused by yields in pcalls. This does not affect the calling thread, but the error message is shown.

Easy repro:

pcall(function() error'Sad!' end) -- no error
pcall(function() wait() error'Sad-der!' end) -- error, but thread continues
3 Likes