Responses in route handlers
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Hi all, I'm kindof new to nextjs and trying to create a post api endpoint for signup.
Not sure if I'm using the route handlers correctly.
The error response for example for status 422 does not get caught when making a fetch request. What am I doing wrong?
Not sure if I'm using the route handlers correctly.
The error response for example for status 422 does not get caught when making a fetch request. What am I doing wrong?
// pages/api/auth/signup.ts
import type { NextApiRequest, NextApiResponse } from "next";
import { hash } from "bcryptjs";
import { prisma } from "../../../lib/prisma"; // Adjust the import path according to your project structure
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: Request, res: Response) {
if (req.method !== "POST") {
return new Response("Method Not Allowed", { status: 405 });
}
const { email, password, name } = await req.json();
console.log("entering POST function");
console.log("GOT through if statemetn");
console.log("got body", req.body);
if (!email || !password || !email.includes("@")) {
return new Response("Invalid input", { status: 422 });
}
console.log("got through validation 2");
const existingUser = await prisma.user.findUnique({
where: {
email: email,
},
});
if (existingUser) {
return new Response("User already exists", {
status: 422,
});
}
const hashedPassword = await hash(password, 12);
const createdUser = await prisma.user.create({
data: {
email,
name,
isGuest: false,
password: hashedPassword,
},
});
return new Response("User created", { status: 201 });
}Answered by Giant panda
Afetch()promise only rejects when a network error is encountered (which is usually when there's a permissions issue or similar). Afetch()promise does not reject on HTTP errors (404, etc.).
3 Replies
Giant panda
Afetch()promise only rejects when a network error is encountered (which is usually when there's a permissions issue or similar). Afetch()promise does not reject on HTTP errors (404, etc.).
Answer
Giant panda
You'd have to assert that the status code of the response was positive instead of relying on the promise being rejected.
Transvaal lionOP
ah okay, thanks Near