4

I have a simple try/catch block in my SuiteScript:

try
{
    var csvFile = file.create({name : outputFileName,
                               contents : header,
                               folder : 123,
                               fileType : 'xy'});
}
catch (e)
{
 log.debug({
    title : 'Error',
    details : e.description
  });
}

(note that in the above the fileType of 'xy' is intentional, to force the error).

The problem is that when I get to the catch block, 'e' is always undefined. That's how it's logged and that's what the debugger shows. I don't know how that can even happen.

What could be causing this?

Bud
  • 709
  • 2
  • 11
  • 28
  • 1
    I am seeing the same thing in a 2.1 suitelet I have created and have yet to be able to figure it out. the "e" is not defined when i get into the catch. I wondered if any solution was ever determined for this? – jvoigt Nov 28 '22 at 15:12

2 Answers2

1

Could it be that e is defined but e.description is not? Netsuite uses e.name and e.message for their custom SuiteScriptError objects (not e.description). These can be logged by using dot notation or just with JSON.stringify(e). If it is just a JavaScript Error object (triggered by a syntax error for example), then JSON.stringify won't work, but you can still use e.name and e.message.

  • 1
    Sadly no, it's ```e``` that's not defined. – Bud May 18 '21 at 23:46
  • 1
    @Bud : if `e` was undefined, accessing `e.description` would trigger an exception. Do you see an exception ? or do you see a message, which is logged, and states `..., details: undefined` ? – LeGEC May 31 '21 at 21:23
1

This may or may not be a solution for you, but there is a free set of SuiteScript abstractions specifically geared for more helpful error catching: https://github.com/datatek-software/netsuite-patterns

Your code would become:

const fobj = {
 name : outputFileName,
 contents : header,
 folder : 123,
 fileType : 'xy'
}

const create = safeExecute(file.create, fobj);

if (!create.isSuccess) {
   console.error(create.message);
   console.warning(create.stack);
}

If the call is unsuccessful, the isSuccess property will be false and the message property will contain the message exception. If you need it, the stack property will contain the stack trace for the exception.

More: https://datatek.io/2019/04/25/suitescript-with-a-safety-net/

Kinglish
  • 23,358
  • 3
  • 22
  • 43