If I invoke the webhook locally when logged in, this works. When I’m logged out, I get a permission error, as I should and want.
But, the response from the webhook is an Internal server error (caused by the permission error?). Based on my logging, I think the catch block does execute, but the actual webhook response is not what I return, but rather the Internal server error. So, instead of my own custom { message: "Not found" }, I get this:
ERROR System Error: User's errors: [{"message":"You don't have permission to perform this operation","locations":[{"line":2,"column":3}],"path":["user"],"code":"NotAuthorizedError","details":{"permissions":"You don't have permission to perform this operation"}}]
So I know the permission error does happen.
Reproduce the Issue: What steps can someone take to replicate the problem?
Create a webhook that uses ctx.api.gqlRequest to make a query to e.g. the user endpoint
Wrap the gqlRequest() inside a try-catch block
Fire the webhook without being logged in
Expected Behavior: What did you expect to happen?
The webhook response should be whatever I define in my webhook
Webhooks are public functions by default and are not permissioned using 8base’s native authorization system (they don’t know if you’re logged in/out).
So the error is not related to permissions, as you thought, but to the body block structure specifically. In the return { } block body is always a String, not an Object.
So if you want to make it work, you can wrap body: { //any code } to the JSON.stringify() function => body: JSON.stringify ( { //any code } )
and then you will see your custom error message instead of “Internal server error” while deploying your webhook (you can check it here: Workspace → Functions → Webhook → Settings item → Endpoint).
Thanks a lot. Indeed the issue was just because of the wrong type for body. If I stringify the JSON manually, it works as intended. Maybe that’s something the platform could do for the user here, seems like this would be a common developer mistake. Not a big deal though, I can just write a wrapper for my handlers to do this kind of stuff.
Regarding the permissioning, I did read that part about webhooks in the documentation and I’m a bit confused why it says it’s not permissioned using the default system.
When I invoke webhooks and use gqlRequest there, it does seem to know when I’m logged in (when using the CLI or SDK) and the permissioning seems to work fine. Of course when I’m not logged in or send a separate GET request, whatever the webhook does is limited to public permissions.
I use this piece of code to check whether the user is logged in: