Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda