How to return 403 when user is not set in cloud function request?

Hello there!

I would like to write a cloud function that throws an error and a 403 http response code when the request.user is not defined for example.

For now I’m jus throwing an Error but the HTTP code is 400 and I do not really like this.

How can I achieve this?

Thank you!

2 Likes

I believe something like this should work:

const error = new Error();
    error.status = 403;
    error.message = 'unauthorized';
    throw error;

Thanks @davimacedo but response status is still 400 on Postman :frowning:

The closest I got to what you want is this:

throw new Parse.Error(403, 'unauthorized');

But it gives http 200 and an object with code “403”. In Parse JS docs I can’t see any way to do that as well, which is pretty disappointing.

I think it should be possible to let the developer determine the http response code to return in a Cloud Function.

If you feel like open an issue in the Parse Server repo and possibly look into how this can be achieved in code, I’ll be happy to support.

Does anyone know if this is possible now? Seems to be a pretty common use case for any web applications. The vast majority of front ends heavily make use of HTTP codes.

There is no way to return 400 codes to the parse-server as far as I know-

1 Like

Just a note, in order for us to prioritize this feature request it would be good if someone could open a feature request in the Parse Server GitHub repository.

What is it ? Is there a parse server functionality, I don’t think so

@youcineofficial1122 I don’t think that sets the code in the HTTP header. It is a workaround that requires to manually parse and handle the information.

@Manuel I’ve created a feature request here: Return HTTP response status codes from Cloud Code · Issue #9479 · parse-community/parse-server · GitHub

1 Like