2
0
Selaa lähdekoodia

为所有路由添加了ErrorSchema和application/json格式的错误响应

yourname 8 kuukautta sitten
vanhempi
sitoutus
610a729ef7
2 muutettua tiedostoa jossa 42 lisäystä ja 6 poistoa
  1. 25 3
      src/server/api/auth.ts
  2. 17 3
      src/server/api/user.ts

+ 25 - 3
src/server/api/auth.ts

@@ -5,6 +5,7 @@ import { User } from '../modules/users/user.entity'
 import { z } from 'zod'
 import { HTTPException } from 'hono/http-exception'
 import { AuthContext, authMiddleware } from '../middleware/auth.middleware'
+import { ErrorSchema } from '../middleware/errorHandler'
 
 
 
@@ -64,7 +65,14 @@ const loginRoute = createRoute({
         }
       }
     },
-    401: { description: '用户名或密码错误' }
+    401: {
+      description: '用户名或密码错误',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    }
   }
 })
 
@@ -96,7 +104,14 @@ const registerRoute = createRoute({
         }
       }
     },
-    400: { description: '用户名已存在' }
+    400: {
+      description: '用户名已存在',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    }
   }
 })
 
@@ -124,7 +139,14 @@ const meRoute = createRoute({
         }
       }
     },
-    401: { description: '未授权' }
+    401: {
+      description: '未授权',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    }
   }
 })
 

+ 17 - 3
src/server/api/user.ts

@@ -166,7 +166,14 @@ const updateUserRoute = createRoute({
         }
       }
     },
-    404: { description: 'User not found' }
+    404: {
+      description: 'User not found',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    }
   }
 });
 
@@ -176,7 +183,7 @@ const updateUserHandler = app.openapi(
     const { id } = c.req.valid('param');
     const data = c.req.valid('json');
     const user = await userService.updateUser(parseInt(id), data);
-    if (!user) return c.notFound();
+    if (!user) return c.json({ code: 404, message: 'User not found' }, 404);
     return c.json(user, 200);
   }
 );
@@ -193,7 +200,14 @@ const deleteUserRoute = createRoute({
   },
   responses: {
     204: { description: 'No Content' },
-    404: { description: 'User not found' }
+    404: {
+      description: 'User not found',
+      content: {
+        'application/json': {
+          schema: ErrorSchema
+        }
+      }
+    }
   }
 });