Back

Internal Runtime error

  • 0
  • Functions
ski
16 Jul, 2023, 09:28

How do I fix internal runtime error causing my function to failed

TL;DR
The user is experiencing an internal runtime error causing their function to fail. They have provided an updated code with some formatting suggestions. They also mentioned the function is running for 23ms with a timeout of 900ms. The suggested solution is to add a try/catch block to handle exceptions in the code, preventing it from throwing an exception and causing a runtime error. This should help fix the issue.
Drake
16 Jul, 2023, 15:42

How long does the function run for and what's the timeout on the function?

ski
16 Jul, 2023, 16:11

It run for 23ms and the max timeout is 900 basically the default appwrite timeout

Drake
16 Jul, 2023, 16:12

So this typically happens when an exception is thrown by your code. Try to add try/catch to prevent your code from throwing an exception

ski
16 Jul, 2023, 16:14

i do have that in my code from appwrite.client import Client from appwrite.services.databases import Databases

def main(req, res): try:

TypeScript
client = Client()

if not req.variables.get('APPWRITE_FUNCTION_ENDPOINT') or not req.variables.get('APPWRITE_FUNCTION_API_KEY'):
    print('Environment variables are not set. Function cannot use Appwrite SDK.')
else:
    client.set_endpoint(req.variables.get('APPWRITE_FUNCTION_ENDPOINT', None))
    client.set_project(req.variables.get('APPWRITE_FUNCTION_PROJECT_ID', None))
    client.set_key(req.variables.get('APPWRITE_FUNCTION_API_KEY', None))
    client.set_self_signed(True)

    # Create the database instance
    database = Databases(client)

    # Retrieve the collection and database IDs from environment variables
    collection_id = req.variables.get('APPWRITE_FUNCTION_COLLECTION_ID', None)
    database_id = req.variables.get('APPWRITE_FUNCTION_DATABASES_ID', None)

    if collection_id and database_id:
        Extraction(req.payload, database, database_id, collection_id)
    else:
        print('Collection or Database environment variables not set.')

return res.json({
    "areDevelopersAwesome": True
})

def Extraction(document, database, database_id, collection_id): id = document['$id'] name = document['name'] email = document['email']

TypeScript
# Use the database and collection IDs to create a document with the same ID as the 'id' field
usersDocument = database.create_document(
    database_id,
    collection_id,
    document_id=id,  # Set the document ID to the value of the 'id' field
    data={
        'id': id,
        'name': name,
        'email': email
    }
)

except Exception as e: print(e) return res.json({...})

Drake
16 Jul, 2023, 16:15

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

Drake
16 Jul, 2023, 16:16

Instead of printing e, try to print str(e)

Drake
16 Jul, 2023, 16:17

Is the indentation at the end wrong from copying and pasting? What's in the res.json() at the end?

ski
16 Jul, 2023, 16:28

Yes it did copy from a previous support ticket with similar problem, I remove it and except Exception as e: print (‘An error occurred:’, str(e)) still getting the same error

Drake
16 Jul, 2023, 16:45

What's the full updated code now? Please make sure to format it using back ticks.

How long is it running for?

ski
16 Jul, 2023, 17:05

here is the updated code btw am not really sure about using back ticks and it running 3ms

ski
16 Jul, 2023, 17:05

from appwrite.client import Client from appwrite.services.databases import Databases

def main(req, res): try:

TypeScript
client = Client()

if not req.variables.get('APPWRITE_FUNCTION_ENDPOINT') or not req.variables.get('APPWRITE_FUNCTION_API_KEY'):
    print('Environment variables are not set. Function cannot use Appwrite SDK.')
else:
    client.set_endpoint(req.variables.get('APPWRITE_FUNCTION_ENDPOINT', None))
    client.set_project(req.variables.get('APPWRITE_FUNCTION_PROJECT_ID', None))
    client.set_key(req.variables.get('APPWRITE_FUNCTION_API_KEY', None))
    client.set_self_signed(True)

    # Create the database instance
    database = Databases(client)

    # Retrieve the collection and database IDs from environment variables
    collection_id = req.variables.get('APPWRITE_FUNCTION_COLLECTION_ID', None)
    database_id = req.variables.get('APPWRITE_FUNCTION_DATABASES_ID', None)

    if collection_id and database_id:
        Extraction(req.payload, database, database_id, collection_id)
    else:
        print('Collection or Database environment variables not set.')

return res.json({
    "areDevelopersAwesome": True
})

def Extraction(document, database, database_id, collection_id): id = document['$id'] name = document['name'] email = document['email']

TypeScript
# Use the database and collection IDs to create a document with the same ID as the 'id' field
usersDocument = database.create_document(
    database_id,
    collection_id,
    document_id=id,  # Set the document ID to the value of the 'id' field
    data={
        'id': id,
        'name': name,
        'email': email
    }
)

except Exception as e: print('An error occurred:', str(e))

Drake
16 Jul, 2023, 17:06
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