Next.js Discord

Discord Forum

Handle endpoint or api isn't found in API routes

Unanswered
Bigheaded ant posted this in #help-forum
Open in Discord
Bigheaded antOP
Hello everyone, Does anyone know about how we can handle endpoint or api that doesn't exist in the api routes. So when it's not found I want to send some specific response. Thank you.

18 Replies

Bigheaded antOP
Uppp please
@Bigheaded ant Hello everyone, Does anyone know about how we can handle endpoint or api that doesn't exist in the api routes. So when it's not found I want to send some specific response. Thank you.
app/api/[[…not-found]]/route.ts

Send the response you want here.


But… in the first place why do you want to do this?
@joulev app/api/[[…not-found]]/route.ts Send the response you want here. But… in the first place why do you want to do this?
Bigheaded antOP
Because when I deployed on production, and then there's an error from the server, the response that given is not informative.
So I want to modify to be able more informative.
@Bigheaded ant Because when I deployed on production, and then there's an error from the server, the response that given is not informative.
You don’t need a lot of info, you only need a 404 status code. Which is already done by next, so the [[…not-found]] is not needed
The message error comes from the database and not from the Next or not found
@joulev You don’t need a lot of info, you only need a 404 status code. Which is already done by next, so the [[…not-found]] is not needed
Bigheaded antOP
Oh yeah sir, I think this is not working. Or am I doing it wrong?
I want do something like this.
@Bigheaded ant The message error comes from the database and not from the Next or not found
That sounds like a bug in your code. Do you know what api route is that message coming from?
@joulev That sounds like a bug in your code. Do you know what api route is that message coming from?
Bigheaded antOP
I think it's from the route of /pages/api that we've created right.
@Bigheaded ant I think it's from the route of /pages/api that we've created right.
no it's not related. if information about the db shows up in a response, the response must have come from an api route that is connected to that database
@joulev no it's not related. if information about the db shows up in a response, the response must have come from an api route that is connected to that database
Bigheaded antOP
Yeah it's because the result of response is not valid, that's why I got the error. But the error from the response and and browser that hit the endpoint is different.
@joulev app/api/[[…not-found]]/route.ts Send the response you want here. But… in the first place why do you want to do this?
Bigheaded antOP
Now I've already implemented this, so whenever the endpoint is not found then I will send the message that looks more informative like 'The API route is not found'.
@joulev Uh you are using the pages router so ignore it, that one is for the app router only
Bigheaded antOP
Yeah, I thought it's working in pages router too.
Thank you sir.