Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
How to use a user's submitted code, and get its errors to have relevant info?

So I'm creating a mod for the singleplayer browser game Cookie Clicker. In my mod I allow the user to insert in their own code to do their own special things to interact with my mod's main function.

However, when the user codes on my custom editor, I want to "test" their code before they save to make sure no errors happen, and if they do, display a error message with what they did and where they did it. Getting the error is easy with a try/catch. But I noticed the error message is:

SynaxError: missing ) after argument list
    at new Function (<anonymous>)
    at HTMLAnchorElement.save.onclick (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/userscript.html?name=Building%2520Sorter.user.js&id=18320655-b018-42e2-8fa5-7fb0cc8d2d70:578:24)

Which isn't helpful for me at all. The most I could salvage from this is the first line. However, that doesn't tell the user at all where the error is located in their code.

the 578:24 that points to the supposed error is:

try{
    //code.value is a STRING of the user's code
    let func = new Function(code.value);//<-- error points here in my source code.
    func.call(null, [[0, 1, 2], Game]);
    save.classList.remove('ModBuildingSorter_unsaved');
}
catch(e){
    console.dir(e);
}

What I would like to happen is when the user sumbits:

return function(array){
    return array.sort(function(building1,building2){
        return building1.price - building2.price;
    };// missing array.sort closing parenthesis
}

get's ran, I can get a syntax error telling me it's on line 4

Is there a way I can do this? Make the user's code act kinda like it's own file and try running it so I can find out which row & column the error is located?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could, in theory, run the function from an eval()

i.e.:

try {
    let a = "function test(o){console.lo(o)}test('hello');" // Minified function
    eval(a)
} catch (e) {
    console.log(e)
}

Here is the unminified function for example purposes:

function test(o)
{
console.lo(o) // <-- Error
}

test('hello');

and this returns the error correctly, which is

TypeError: console.lo is not a function
    at test (eval at <anonymous> (D:\StackOverflowSandbox\index.js:3:5), <anonymous>:1:26)

Hope I've helped.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!