Reputation: 10065
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"
I'am not sure it's the best way to create a clean API with cloud function.
Do you have suggestions?
Upvotes: 6
Views: 2147
Reputation: 83191
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:
INVALID_ARGUMENT
Error CodeALREADY_EXISTS
Error CodeThe full list is to be found here. Have also a look at the following section of the doc.
Upvotes: 7