17
votes

I'm creating a firebase application which uses firebase-cloud-functions.

index.js

exports.auth = functions.https.onRequest((request, response) => {
  response.status(200).send({
    status : "Some Status"
  });
}

This is very simple functions. I want to make a POST request on the endpoint with some payload. When I tested the API using Firebase Cloud Function Emulator and POSTman with bad json

{
    "phoneNumber: "9632725300"
}

The server just crashed! My question is how to handle the bad request in firebase functions like these.

with this error enter image description here

3
"The server just crashed!" How did the server crash? What's the error message? - Frank van Puffelen

3 Answers

4
votes

The server did not crash. You have sent it a bad request (malformed JSON) and it responded perfectly with a status code 400 which is "Bad Request".

You'd rather correct your JSON...

EDIT:

If you really wanted to be able to send invalid JSON, you could do so by circumventing the JSON body parser. To do so, you could either change your request to have a content-type header set to "text/plain". This content-type will use the text body parser, which will not parse any JSON.

Note that doing so will require you to handle the JSON parsing yourself, but will permit to handle to error yourself using a try-catch.

let json;
try {
    json = JSON.parse(json);
} catch (e) {
    // Handle JSON error.
}

Taken from https://firebase.google.com/docs/functions/http-events

1
votes

What you're experiencing is not actually a server crash. In fact, technically, by using Cloud Functions, you don't have a server to crash. (For this reason they're called "Serverless Infrastructure") Each request / operation you perform on Cloud Functions is kind of like a brand new server. Which is actually what's fantastic about Cloud Functions in general. (This is an overly simplified explanation, I'd suggest reading up a bit more about it for a better in depth explanation)

That being said, from what I understand you're trying to figure out if the JSON you got is invalid (bad) or not. Occasionally, when I have to hook up a bunch of external services, rarely, but sometimes, they return a bad JSON that my Cloud Functions can't parse, therefore throws an error.

The solution is to put your JSON.parse in to a separate function and a try / catch block like this:

function safelyParseJSON (json) {
  var parsed;

  try {
    parsed = JSON.parse(json);
  } catch (e) {
    // BAD JSON, DO SOMETHING ABOUT THIS HERE.
  }

  return parsed; // will be undefined if it's a bad json!
}

function doSomethingAwesome () {
  var parsedJSON = safelyParseJSON(data);
  // Now if parsedJSON is undefined you know it was a bad one, 
  // And if it's defined you know it's a good one. 
}

With this helper function, if you have to deal with a lot of external JSON resources, you can easily determine if the JSON you're trying to parse is good, and if not, you can at least handle the error your way.

Hope this helps :)

-1
votes

{\n\t"phoneNumber: "9632725300"\n}

From the screenshot, I see that the JSON is invalid or malformed. It contains newline (\n) and tab space (\t) characters. Also, the key "phoneNumber" is not wrapped in double quotes, which again invalidates the JSON.

Here's a valid format of the JSON that the server should receive

{
    "phoneNumber": "9632725300"
}