I am implementing the following cloud function
exports.getRandomInteger = functions
.region(process.env.REGION)
.runWith({ memory: "2GB", timeoutSeconds: 120 })
.https.onRequest((req, res) => {
return cors(req, res, async () => {
try {
validateHttpMethod(req, "GET");
const authToken = getIdTokenFromRequest(req);
await verifyIdToken(authToken);
const {
lowerLimit,
upperLimit,
} = validateGetRandomIntegerParams(req.query);
const randomInteger = Math.floor(
Math.random() * (upperLimit - lowerLimit + 1)
) + lowerLimit;
res.status(200).send(randomInteger.toString());
} catch (err) {
if (err instanceof functions.https.HttpsError) {
throw err;
}
// An unknown error has occurred
functions.logger.error(err);
res.status(400).send(err);
}
});
});
Inside my validateHttpMethod(req, "GET") function I am throwing a functions.HttpsError if the request method is not equal to “GET”, like this:
throw functions.https.HttpsError(
"invalid-argument",
"Invalid HTTP method.",
{
status: "error",
code: "http/invalid-method",
message: "The provided HTTP method is not valid.",
}
);
Is this OK? Should I use “invalid-argument”? Or should I throw the error like this?
res.status(400).send("HTTP method must be GET");