How do I print the string of where the error went in a pcall function?

Hi there, I have a custom code editor that uses Lexer and highlights the tokens. Sometimes it can throw a confusing error saying string expected, got string. I tried to use a pcall function to print out where in the string the error went but I am having trouble as I am using table.concat along with a list of values to set the text property of my textlabel.

local lines
	if ishighlighting == false then
		lines = string.split(text,"\n")
	else
		lines = string.split(highlight(text),"\n")
	end

	local concat
	local succ,err = pcall(function()
		concat = table.concat(stringtable, "\n")
		TextBox.Text = concat
	end)
	if err then
		print(error())
	end

	task.wait()

It’s actually pretty simple: print(err) Alternatively, you can do error(err). That will print a red error notice in the server output and it will halt the script that threw it. warn() will print a warning notice. In your code print(error()) is, well, an error because error is a print function in of itself.

I tried this print(error(lines[#lines],1)) which seems to print where it cut off, assuming thats where the error went as well as the err itself and got this
image

Just also asking about my post below, but would it work if i printed the type of the line?

Alright, so I did this

warn(type(lines[#lines]))
warn(#lines..": ",err)
print(error(lines[#lines],1))

and it prints “string”, as the original error says string expected, got string. I assume there’s some sort of difference between the two.

I just edited the post to fix up a missing line but I’m referring to the first line of that code block

You loaded it to use err … so you use err to print it out.

print(err) or warn(err)

I did, right here warn(#lines..": ",err)

I’ll just mark your post as the solution since it solves the original thread anyway

You can also rem out the pcall and let it error on its own …

It doesn’t work like that. pcall() returns a tuple, which you have accounted for in local succ,err = pcall(function() ..... end). print(), warn(), error() all print stuff. Do not nest an error() call inside a print(), it will not work the way you expect.