Commit 9ce8b9d 1 parent 799308e commit 9ce8b9d Copy full SHA for 9ce8b9d
File tree 2 files changed +19
-4
lines changed
2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { expect, test } from "@jest/globals";
5
5
import { dbMock } from "@/test/dbMock" ;
6
6
import { authMock } from "@/test/authMock" ;
7
7
import { UserType } from "@prisma/client" ;
8
+ import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library" ;
8
9
9
10
test ( "returns 401 on unauthenticated requests" , async ( ) => {
10
11
await testApiHandler ( {
@@ -146,8 +147,16 @@ test("user already exists", async () => {
146
147
} ) ;
147
148
148
149
dbMock . user . create . mockImplementation ( ( ) => {
149
- throw new Error ( ) ;
150
- } )
150
+ throw new PrismaClientKnownRequestError (
151
+ 'violates uniqueness constraint' ,
152
+ {
153
+ code : 'P2002' ,
154
+ clientVersion : 'mock' ,
155
+ meta : { } ,
156
+ batchRequestIdx : 1
157
+ }
158
+ ) ;
159
+ } ) ;
151
160
152
161
const res = await fetch ( { method : "POST" , body : getGoodFormData ( ) } ) ;
153
162
await expect ( res . status ) . toEqual ( 409 ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { UserType } from "@prisma/client";
5
5
import { NextRequest , NextResponse } from "next/server" ;
6
6
import { zfd } from "zod-form-data" ;
7
7
import * as argon2 from 'argon2' ;
8
+ import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library" ;
8
9
9
10
const ALLOWED_USER_TYPES : UserType [ ] = [
10
11
UserType . ADMIN ,
@@ -79,8 +80,13 @@ export async function POST(req: NextRequest) {
79
80
type : userInvite . userType
80
81
}
81
82
} ) ;
82
- } catch {
83
- return conflictError ( "User already exists" ) ;
83
+ } catch ( e ) {
84
+ if ( e instanceof PrismaClientKnownRequestError ) {
85
+ if ( e . code === 'P2002' ) {
86
+ return conflictError ( "User already exists" ) ;
87
+ }
88
+ }
89
+ throw e ;
84
90
}
85
91
return ok ( ) ;
86
92
}
You can’t perform that action at this time.
0 commit comments