NASEER ULLAH
NASEER ULLAH

Reputation: 97

500 (Internal Server Error) in Nextjs Vercel

Remeber Everything work fine locally on localhost:3000

At first I was getting this error " Application error: a server-side exception has occurred (see the server logs for more information). Digest: 2333662928 "

But after some changes in src\lib\payload-utils.ts with the help of chatgpt. I change from this

import { User } from '../payload-types'
import { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies'
import { NextRequest } from 'next/server'

export const getServerSideUser = async (
  cookies: NextRequest['cookies'] | ReadonlyRequestCookies
) => {
  const token = cookies.get('payload-token')?.value

  const meRes = await fetch(
    `${process.env.NEXT_PUBLIC_SERVER_URL}/api/users/me`,
    {
      headers: {
        Authorization: `JWT ${token}`,
      },
    }
  )

  const { user } = (await meRes.json()) as {
    user: User | null
  }

  return { user }
}

To This

import { User } from "../payload-types";
import { ReadonlyRequestCookies } from "next/dist/server/web/spec-extension/adapters/request-cookies";
import { NextRequest } from "next/server";

export const getServerSideUser = async (
  cookies: NextRequest["cookies"] | ReadonlyRequestCookies
): Promise<{ user: User | null }> => {
  try {
    const token = cookies.get("payload-token")?.value;

    if (!token) {
      console.error("No token found in cookies");
      return { user: null };
    }

    const meRes = await fetch(
      `${process.env.NEXT_PUBLIC_SERVER_URL}/api/users/me`,
      {
        headers: {
          Authorization: `JWT ${token}`,
        },
      }
    );

    if (!meRes.ok) {
      console.error(
        `Failed to fetch user: ${meRes.status} ${meRes.statusText}`
      );
      return { user: null };
    }

    const data = await meRes.json();
    const user = data.user as User | null;

    return { user };
  } catch (error) {
    console.error("An error occurred while fetching the user:", error);
    return { user: null };
  }
};

So now I can see the homepage of my website on vercel domain. But now another issue pop out that is " Failed to load resource: the server responded with a status of 500 ()Understand this error layout-276a15fa0a01ecfc.js:1

   GET https://digitalproducts.vercel.app/api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D 500 (Internal Server Error)
enter code here

" Vercel logs Shows this

"

Aug 10 10:16:58.30
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:16:58.14
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:16:53.50
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:16:53.37
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:16:52.53
GET
---
digitalproducts.vercel.app
/
No token found in cookies
Aug 10 10:16:52.45
GET
200
digitalproducts.vercel.app
/
Aug 10 10:16:50.78
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:16:50.65
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:16:48.80
GET
200
digitalproducts.vercel.app
/
Aug 10 10:16:48.80
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:16:48.80
GET
200
digitalproducts.vercel.app
/sign-in
Aug 10 10:16:48.79
GET
200
digitalproducts.vercel.app
/sign-up
Aug 10 10:16:48.79
GET
200
digitalproducts.vercel.app
/products
Aug 10 10:16:48.79
GET
200
digitalproducts.vercel.app
/products
Aug 10 10:16:48.69
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:16:47.43
GET
---
digitalproducts.vercel.app
/
No token found in cookies
Aug 10 10:16:47.34
GET
200
digitalproducts.vercel.app
/
Aug 10 10:12:03.44
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:12:03.12
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:12:01.46
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:12:01.22
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:11:23.78
GET
---
digitalproducts.vercel.app
/api/users/me
No token found in cookies
Aug 10 10:11:23.76
GET
404
digitalproducts.vercel.app
/api/users/me
[GET] /api/users/me status=404
Aug 10 10:11:23.56
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:11:23.43
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:11:21.49
GET
---
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
⨯ Error: No response is returned from route handler '/vercel/path0/src/app/api/trpc/[trpc]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler. at /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39688 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Aug 10 10:11:21.48
GET
500
digitalproducts.vercel.app
/api/trpc/getInfiniteProducts
[GET] /api/trpc/getInfiniteProducts?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A4%2C%22query%22%3A%7B%22sort%22%3A%22desc%22%2C%22limit%22%3A4%7D%7D%7D&nxtPtrpc=getInfiniteProducts status=500
Aug 10 10:11:21.37
GET
---
digitalproducts.vercel.app
/
No token found in cookies
Aug 10 10:11:21.35
GET
200
digitalproducts.vercel.app
/
Aug 10 10:11:19.88
GET
---
digitalproducts.vercel.app
/
No token found in cookies

"

My .env

PAYLOAD_SECRET=
MONGODB_URL=
NEXT_PUBLIC_SERVER_URL=
RESEND_API_KEY=
STRIPE_SECRET_KEY=
STRIPE_WEBHOOK_SECRET=

I an not sign in and can not sign up

    layout-276a15fa0a01ecfc.js:1 
 POST https://digitalproducts.vercel.app/api/trpc/auth.createPayloadUser?batch=1 500 (Internal Server Error)
fetch   @   layout-276a15fa0a01ecfc.js:1
fetchHTTPResponse   @   932-94a1897485bc8a1a.js:17
await in fetchHTTPResponse      
(anonymous) @   932-94a1897485bc8a1a.js:17
httpRequest @   932-94a1897485bc8a1a.js:17
jsonHttpRequester   @   932-94a1897485bc8a1a.js:17
(anonymous) @   932-94a1897485bc8a1a.js:17
dispatch    @   932-94a1897485bc8a1a.js:17
setTimeout      
load    @   932-94a1897485bc8a1a.js:17
(anonymous) @   932-94a1897485bc8a1a.js:17
subscribe   @   932-94a1897485bc8a1a.js:17
(anonymous) @   932-94a1897485bc8a1a.js:17
subscribe   @   932-94a1897485bc8a1a.js:17
startIfNeeded   @   932-94a1897485bc8a1a.js:17
subscribe   @   932-94a1897485bc8a1a.js:17
(anonymous) @   932-94a1897485bc8a1a.js:17
observableToPromise @   932-94a1897485bc8a1a.js:17
requestAsPromise    @   932-94a1897485bc8a1a.js:17
mutation    @   932-94a1897485bc8a1a.js:17
mutationFn  @   932-94a1897485bc8a1a.js:17
fn  @   932-94a1897485bc8a1a.js:17
run @   932-94a1897485bc8a1a.js:17
createRetryer   @   932-94a1897485bc8a1a.js:17
(anonymous) @   932-94a1897485bc8a1a.js:17
execute @   932-94a1897485bc8a1a.js:17
await in execute        
mutate  @   932-94a1897485bc8a1a.js:17
(anonymous) @   932-94a1897485bc8a1a.js:17
(anonymous) @   page-243eb4b511759344.js:1
(anonymous) @   834-9df09acf20fb67ff.js:1
await in (anonymous)        
Hi  @   fd9d1056-3e65c361eee07ad7.js:9
Ni  @   fd9d1056-3e65c361eee07ad7.js:9
Oi  @   fd9d1056-3e65c361eee07ad7.js:9
Nn  @   fd9d1056-3e65c361eee07ad7.js:9
Zm  @   fd9d1056-3e65c361eee07ad7.js:9
(anonymous) @   fd9d1056-3e65c361eee07ad7.js:9
Ak  @   fd9d1056-3e65c361eee07ad7.js:9
ll  @   fd9d1056-3e65c361eee07ad7.js:9
zm  @   fd9d1056-3e65c361eee07ad7.js:9
xm  @   fd9d1056-3e65c361eee07ad7.js:9
wm  @   fd9d1056-3e65c361eee07ad7.js:9

Upvotes: 1

Views: 130

Answers (1)

waleed.
waleed.

Reputation: 50

Error: No response is returned from route handler. 
Ensure you return a `Response` or a `NextResponse` in all branches of your handler.

The error itself is pretty clear.

Have you checked your /api folder and ensured that all the routes are returning something?

Even if you are returning a Response or Next Response, it may get stuck in a code path like if, else, switch, try, catch, and finally.

As an example, following has exact same issue.

import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
    if (req.url === "example.com") {
        return NextResponse.json({data: "Success!"}, {status: 200})
    }
}

Now, if the req.url isn't example.com, you will get the following:

Error: No response is returned from route handler 
'/Users/waleed/Desktop/VS Code/Test/app/api/test/route.ts'. 
Ensure you return a `Response` or a `NextResponse` in all branches of your handler.

We can fix this by giving a default response like this:

import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
    if (req.url === "example.com") {
        return NextResponse.json({data: "Success!"}, {status: 200})
    }

    // Default response if all else fails
    return NextResponse.json({data: "Uh oh! Resource Not Found"}, {status: 404})
}

It's always a good habit to have a default response.

Another really important thing that is very wrong here is the lack of error handling. If there was something to catch errors here, then at least your app wouldn't break to this extent.

Upvotes: 0

Related Questions