Firebase HTTP Cloud Function - 500 Server Error, try again

Viewed 4571

I have an HTTP function executed a few times a second (I don't have exact statistics).

Sometimes, the request will return data like intended, and sometimes it will return this error text:

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>The server encountered an error and could not complete your request.<p>Please try again in 30 seconds.</h2>
<h2></h2>
</body></html>

When this happens, this tends to happen for all of my HTTP functions. Making the same request 5 minutes later often works.

Now, I could have imagined its an error in my code, but I am using a middleware to catch all errors and respond as JSON!

Here is my exported cloud function:

const app = express();
app.use(cors({origin: true, credentials: true})); // Allow cross-origin requests
app.get('/', this.reportRequest.bind(this));
app.use(errorMiddleware); // Handle errors

export const report = functions.https.onRequest(app);

And here is the errorMiddleware:

export const errorMiddleware: ErrorRequestHandler = async (e, req, res, next) => {
  const executionId = req.header('function-execution-id');

  const message = 'message' in e ? e.message : e;

  const code = e.code && e.code > 200 && e.code < 600 ? e.code : 500;
  res.status(code).json({message, executionId});

  next();
};
2 Answers

Please check here and star it in order to receive updates regarding the status of this issue.

Some people agree with you that the error is happening somewhere in the middleware even before it hits the function and others believe that requests seem to be load-balanced to instances before they are able to respond.

A workaround would be to implement exponential backoff that identifies this error message and retries after incrementally longer time delays.

There is no ETA for the fix at this time.

I've just met the same Issue, I need to upgrade the memory of my function from 2Go to 8Go, and after this upgrade I received 500 error on 30% of my invocation (with no error in the google log)

I rollback to 2Go and I don't get 500 error anymore...

Related