4
votes

I'm building an API for my project and I'd like to throw custom error to catch it in my application.

Following the documentation, I throw error with the code "unknown"

 throw new functions.https
  .HttpsError("unknown",
    "this is my error message", 
    {code :'myCustomCode', 
     message :'my custom message'}
   )

Then, I catch with the right message and details, but with a"Internal Server Error"

enter image description here

I'am not sure it's the best way to create a clean API with cloud function.

Do you have suggestions?

1

1 Answers

5
votes

With an UNKNOWN Functions error code you will always get a 500 HTTP response status code.

By using a more meaningful error code, among the ones proposed by the Protocol specification for https.onCall (and also here), you can get different response status codes, like, for example:

  • 400 Bad Request for the INVALID_ARGUMENT Error Code
  • 409 Conflict for the ALREADY_EXISTS Error Code
  • etc...

The full list is to be found here. Have also a look at the following section of the doc.