Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Bad JSON in Firebase Cloud Functions?

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

like image 566
Raman Choudhary Avatar asked Jul 26 '17 19:07

Raman Choudhary


2 Answers

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

like image 186
Salketer Avatar answered Sep 27 '22 23:09

Salketer


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 :)

like image 39
johnozbay Avatar answered Sep 27 '22 23:09

johnozbay