Back

[SOLVED] My functions not working again 500 internal error

  • 0
  • Functions
Mosh Ontong
6 Dec, 2023, 19:30

My function is working properly before, and I am using postman to test this and yeah it gives me a good results. But currently, it suddenly not wokring. It does not even showing the logs, and the error giving me an object

TL;DR
The user was experiencing a 500 internal error with their functions. They discovered that the error was occurring because of a certain part of their code. They also mentioned that the error did not show up in Visual Studio Code but had to manually check their files for errors. They moved the log function to the top but were still not receiving the logs on the console. They shared the updated code which included changes in the middleware function. They mentioned that their code is complex and running on the cloud. They were unsure about the cause of the issue and asked for help. Solution: It is not clear from the provided information what caused the issue.
Drake
6 Dec, 2023, 19:31

What's your code? Is this on cloud?

Mosh Ontong
6 Dec, 2023, 23:31

my code is complex and yes cloud

D5
6 Dec, 2023, 23:32

Probably there's something wrong in your code that makes it fail

Mosh Ontong
6 Dec, 2023, 23:33

i ll check

Mosh Ontong
6 Dec, 2023, 23:45
TypeScript
export default async ({ req, res, log, error }: any) => {
  try {
    const client = initializeAppwriteClient();
    await middleware({ req, res, log, error, client });

    return await router.handleRequest({ req, res, log, error, client });
  } catch (e) {
    // Handle initialization errors
    error(`Initialization failed: ${e.message}`);
    return res.send(
      {
        message: `Initialization failed: ${e.message}`,
      },
      500,
      {
        "content-type": "application/json",
      }
    );
  }
};

const middleware = async ({
  req,
  res,
  log,
  error,
  client,
}: {
  res: any;
  req: any;
  log: any;
  error: any;
  client: Client;
}) => {
  const triggered = req.headers["x-appwrite-trigger"];
  const path = req.path;
  const method = req.method;
  const eventType = req.headers["x-appwrite-event"];

  log(`Request received by ${path} with method ${method}`);
  log(`Triggered type: ${triggered}`);
  log(`Event type: ${eventType}`);

  if (triggered === "event" && path === "/" && method === "POST") {
    const eventMap = eventTypeParse(eventType);
    log(`Event map: ${JSON.stringify(eventMap)}`);
    if (getSpecificEventType(log, req, eventType) === "create") {
      if (
        eventMap["collections"] === Bun.env.CONSULTATION_ATTACHMENT &&
        eventMap["documents"]
      ) {
        const { body, path } = await consultationAttachmentBot(
          log,
          client,

          req.body
        );

        log(`Request redirected to ${path}`);

        req.path = path;
        req.body = body;
        return;
      }
    }

    log(`Request redirected to ${req.path}`);
  }
};

const eventTypeParse = (eventType?: string) => {
  if (!eventType) {
    return {};
  }
  const parts = eventType.split(".");
  const map = {};

  for (let i = 0; i < parts.length; i += 2) {
    map[parts[i]] = parts[i + 1];
  }

  return map;
};
Mosh Ontong
6 Dec, 2023, 23:45

logs in the middle should be shown on logs section but it didnt

Mosh Ontong
7 Dec, 2023, 00:04
TypeScript
export default async ({ req, res, log, error }: any) => {
  const triggered = req.headers["x-appwrite-trigger"];
  const path = req.path;
  const method = req.method;
  const eventType = req.headers["x-appwrite-event"];
  log(`Request received by ${path} with method ${method}`);
  log(`Triggered type: ${triggered}`);
  log(`Event type: ${eventType}`);
  try {
    middleware({ req, res, log, error });
    const client = initializeAppwriteClient();

    return await router.handleRequest({ req, res, log, error, client });
  } catch (e) {
    // Handle initialization errors
    error(`Initialization failed: ${e}`);
    return res.send(
      JSON.stringify({
        message: `Initialization failed: ${e}`,
      }),
      500,
      {
        "content-type": "application/json",
      }
    );
  }
};

I moved the log in th top but still not receving the logs on cosole

Mosh Ontong
7 Dec, 2023, 00:32

finnaly its working , the error oocurs because Just of because of this and this kind of error does not show up in vscode I just revisit all my files just to see if there is red error lines, hays typescript things that is why I love dart

Mosh Ontong
7 Dec, 2023, 00:33

[SOLVED] My functions not working again 500 internal error

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