If you are, here is an example code snippet: Firs...
# support-questions
r
If you are, here is an example code snippet: First we throw a custom error in the overrided function:
Copy code
js
Session.init({
        override: {
            functions: (oI) => {
                return {
                    ...oI,
                    createNewSession: async function (_) {
                        // for example purposes, we are always throwing an error...
                        throw new Error("create session failed");
                    }
                }
            }
        }
    })
Then we can catch that in the app's error handler like this:
Copy code
js
app.use((err, req, res, next) => {
    if (err.message === "create session failed") {
        res.setHeader('Content-Type', 'application/json');
        res.end(JSON.stringify({
            status: "GENERAL_ERROR",
            message: "Unable to make new session" // this message will get displayed to the user.
        }));
        return;
    }
    res.status(500).send("Internal error: " + err.message);
});
n
@rp one follow up question to this one. If I have to throw an error during an api override e.g.
emailPasswordSignUpPOST
how can I get a meaningful error message back to the frontend?
r
Once you throw an error in your override function, the error will be propagated to the app’s error handler in which you can send an appropriate response to the frontend.
On the frontend side, you can override the sign up function and catch this 500 error in the original implementation call. Then you can show a message or a popup to the user
We are going to make this much easier in the near future
n
in a nextJS app the app's error handler would be on the backendConfig or the
/api/auth/[[...path]].ts
?
r
Yes.
n
the rest seems straightforward
r
Another option would be to return a response that has status: “FIELD_ERROR”
Along with a the field ID and message
This would display the custom error message against that input field
n
That does seem much simpler
r
Yea. If that works for you, you can use that. Hehe
Initially, I thought you wanted to show a general error on top of the form
n
I anyhow how a custom UI in place, so the error goes where I tell it to 😄
r
Ah fair.
n
however, the backend config is not happy with my return type on the override when I return a FIELD_ERROR it only accepts the "EMAIL_ALREADY_EXISTS_ERROR
I'm sure it would functionally work, because the OG implementation also returns `FIELD_ERROR`s
r
Huh.. that’s odd. Im quite sure the typings in the SDK are fine
n
this is where typescript wants to be smart 😅
r
Oh right.. yea… so that function doesn’t return FIELD_ERROR
Cause the field validation happens before that function is called
So do you want to throw this error based on the value of a particular form field?
n
I have two backend calls which is a initial setup of the user after successful sign up. However If they fail I need to inform the user
r
Ah right. I see. So then it’s better if you follow the first method
Where you throw an error, catch it and then send a custom response to the frontend
n
do you mean catch it around this call? await superTokensNextWrapper( async (next) => { await middleware()(req, res, next) }, req, res )
r
Yes.
n
Gotcha
Thanks
4 Views