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();
};