Browse Source

📝 docs(order): 完善取消订单接口的错误响应格式

- 导入ErrorSchema以规范错误响应结构
- 为所有HTTP错误状态码添加content字段定义
- 统一错误响应格式为{code, message}结构
- 移除旧的{error}响应格式,提升API文档规范性
yourname 1 month ago
parent
commit
b8302a89fc
1 changed files with 12 additions and 8 deletions
  1. 12 8
      packages/orders-module-mt/src/routes/user/cancel-order.mt.ts

+ 12 - 8
packages/orders-module-mt/src/routes/user/cancel-order.mt.ts

@@ -1,6 +1,6 @@
 import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
 import { authMiddleware } from '@d8d/auth-module-mt';
-import { AppDataSource } from '@d8d/shared-utils';
+import { AppDataSource, ErrorSchema } from '@d8d/shared-utils';
 import { AuthContext } from '@d8d/shared-types';
 import { OrderMtService } from '../../services';
 import { CancelOrderRequestDto, CancelOrderResponseDto } from '../../schemas/cancel-order.schema';
@@ -28,16 +28,20 @@ const cancelOrderRoute = createRoute({
       }
     },
     400: {
-      description: '请求参数错误'
+      description: '请求参数错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     403: {
-      description: '订单状态不允许取消'
+      description: '订单状态不允许取消',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     404: {
-      description: '订单不存在'
+      description: '订单不存在',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     500: {
-      description: '服务器内部错误'
+      description: '服务器内部错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     }
   }
 })
@@ -63,19 +67,19 @@ const cancelOrderRoutes = new OpenAPIHono<AuthContext>()
         if (error instanceof Error) {
           if (error.message === '订单不存在') {
             return c.json(
-              { error: error.message },
+              { code: 404, message: error.message },
               404
             );
           } else if (error.message === '当前订单状态不允许取消') {
             return c.json(
-              { error: error.message },
+              { code: 403, message: error.message },
               403
             );
           }
         }
 
         return c.json(
-          { error: error instanceof Error ? error.message : '取消订单失败' },
+          { code: 500, message: error instanceof Error ? error.message : '取消订单失败' },
           500
         );
       }