Handling Errors in Server Components using Try and Catch
Unanswered
Almond stone wasp posted this in #help-forum
Almond stone waspOP
I am trying to use try and catch in a server component but its not working
#This is working
#This is not working
#This is working
const authToCalendly = async () => {
"use server"
const {authUrl} = await getCalendlyAuthCode() as {authUrl?: string | undefined }
redirect(authUrl)
}#This is not working
const authToCalendly = async () => {
"use server"
try{
const {authUrl} = await getCalendlyAuthCode() as {authUrl?: string | undefined }
redirect(authUrl)
}catch(error){
console.log(error)
}
}3 Replies
@Almond stone wasp I am trying to use try and catch in a server component but its not working
#This is working
js
const authToCalendly = async () => {
"use server"
const {authUrl} = await getCalendlyAuthCode() as {authUrl?: string | undefined }
redirect(authUrl)
}
#This is not working
js
const authToCalendly = async () => {
"use server"
try{
const {authUrl} = await getCalendlyAuthCode() as {authUrl?: string | undefined }
redirect(authUrl)
}catch(error){
console.log(error)
}
}
Invoking the redirect() function throws a NEXT_REDIRECT error and terminates rendering of the route segment in which it was thrown.
you need to rethrow the error
you need to rethrow the error
const authToCalendly = async () => {
"use server"
try{
const {authUrl} = await getCalendlyAuthCode() as {authUrl?: string | undefined }
redirect(authUrl)
}catch(error){
console.log(error)
if (error instanceof Error && error.message === 'NEXT_REDIRECT') {
throw error;
}
}
}Almond stone waspOP
okay thanks 😊
@Almond stone wasp okay thanks 😊
please mark solution if it fixed your issue😆