-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
34 lines (30 loc) · 1002 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import { NextResponse } from "next/server";
export function middleware() {
try {
// Let the request continue to the route handler
return NextResponse.next();
} catch (error) {
// https://www.prisma.io/docs/orm/reference/error-reference#error-codes
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === "P2025") {
return NextResponse.json(
{ error: [{ path: "project", message: "Could not find ressource." }] },
{ status: 404 }
);
}
}
const message = error instanceof Error ? error.message : "Internal Server Error";
return NextResponse.json(
{
error: "Internal Server Error",
message: process.env.NODE_ENV === "development" ? message : undefined,
},
{ status: 500 }
);
}
}
// Configure which routes to apply the middleware to
export const config = {
matcher: "/api/:path*",
};