Back

Better debugging when using cloudfunction

  • 0
  • Functions
  • Cloud
Horsty - Cyril (he/him)
5 Aug, 2024, 11:38

I'm facing a "simple" issue

TypeScript
Invalid response. This usually means too large logs or errors. Please avoid logging files or lengthy strings.\nError Code: 500

I think i've found where it's happen due to a JSON.stringify() i try to log.

My code is organise like this

TypeScript
function myFunction({log, error}) {
    log('Start my function');
    try {
        // Do some stuff
        log(`I get this var: ${myVar}`);
        // Do some other stuff that throws an exception
        throw "myException"; // Generate an exception
    } catch (e) {
        error('An error occurred');
        error(`Error message: ${JSON.stringify(e)}`);  // <-- This throw the error due to too long message
    }
}

The issue, is that log before throw exception are not log. So it's very difficult to debug.

TL;DR
Developers are having trouble debugging cloud functions due to long error messages when using `JSON.stringify()`. One workaround is to return content via `res.*` methods without length limits. Another suggestion is to log errors using a separate document, although this may feel like overkill. It's challenging to debug as logs before the exception are not recorded.
Horsty - Cyril (he/him)
5 Aug, 2024, 11:39

It's possible to have better log in this case ? like stacktrace or even all other valid log ?

Horsty - Cyril (he/him)
5 Aug, 2024, 11:40

I'm expected having something like that

TypeScript
Start my function
I get this var: undefined
An error occurred

Invalid response. This usually means too large logs or errors. Please avoid logging files or lengthy strings.\nError Code: 500

So all before log and the log that appwrite send me due to the too large logs

Horsty - Cyril (he/him)
5 Aug, 2024, 11:41

And after that, how i can log the error ? It's possible to return the error ? Or other way ?

TypeScript
res.send(JSON.stringify(e))
darShan
5 Aug, 2024, 11:46

hmm, i don't think doing JSON.stringify(e) should result in that long error message, but it could be possible in some cases. You can return back content via res.* methods. Not sure if that has content length limits but these won't be shown on dashboard/console. One alternative is to use a document to log errors but that feels like an overkill. @Meldiron wdyt?

Horsty - Cyril (he/him)
5 Aug, 2024, 11:48

I will try res.* in a first try to know what is the actual error i have remove the stringify And if it's not working i will try with the document to log the error

Horsty - Cyril (he/him)
5 Aug, 2024, 11:49

i need to wait another error that happens ^^

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more