Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase HTTP Cloud Function - 500 Server Error, try again

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();
};
like image 660
Amit Avatar asked Apr 01 '20 16:04

Amit


People also ask

What is status 500 internal server error?

The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request. This error response is a generic "catch-all" response.

How to check Firebase error?

You can view the reported errors in Error Reporting in the GCP Console. You can also see the errors reported from a particular function when you select it from the list of functions in the GCP Console. Uncaught exceptions produced by your function will appear in Error Reporting.

Why does cloud deployment fail?

Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. Your source code must contain an entry point function that has been correctly specified in your deployment, either via Cloud console or Cloud SDK.

What are firebase cloud functions?

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.


1 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.

like image 159
marian.vladoi Avatar answered Nov 04 '22 21:11

marian.vladoi