Shun Miyazawa 1 год назад
Родитель
Сommit
7022542484

+ 6 - 0
apps/app/src/features/openai/server/routes/delete-ai-assistant.ts

@@ -2,6 +2,7 @@ import { type IUserHasId } from '@growi/core';
 import { ErrorV3 } from '@growi/core/dist/models';
 import type { Request, RequestHandler } from 'express';
 import { type ValidationChain, param } from 'express-validator';
+import { isHttpError } from 'http-errors';
 
 
 import type Crowi from '~/server/crowi';
@@ -51,6 +52,11 @@ export const deleteAiAssistantsFactory: DeleteAiAssistantsFactory = (crowi) => {
       }
       catch (err) {
         logger.error(err);
+
+        if (isHttpError(err)) {
+          return res.apiv3Err(new ErrorV3(err.message), err.status);
+        }
+
         return res.apiv3Err(new ErrorV3('Failed to delete AiAssistants'));
       }
     },

+ 6 - 0
apps/app/src/features/openai/server/routes/update-ai-assistant.ts

@@ -2,6 +2,7 @@ import { type IUserHasId } from '@growi/core';
 import { ErrorV3 } from '@growi/core/dist/models';
 import type { Request, RequestHandler } from 'express';
 import { type ValidationChain, param } from 'express-validator';
+import { isHttpError } from 'http-errors';
 
 import type Crowi from '~/server/crowi';
 import { accessTokenParser } from '~/server/middlewares/access-token-parser';
@@ -56,6 +57,11 @@ export const updateAiAssistantsFactory: UpdateAiAssistantsFactory = (crowi) => {
       }
       catch (err) {
         logger.error(err);
+
+        if (isHttpError(err)) {
+          return res.apiv3Err(new ErrorV3(err.message), err.status);
+        }
+
         return res.apiv3Err(new ErrorV3('Failed to update AiAssistants'));
       }
     },

+ 3 - 3
apps/app/src/features/openai/server/services/openai.ts

@@ -2,12 +2,12 @@ import assert from 'node:assert';
 import { Readable, Transform } from 'stream';
 import { pipeline } from 'stream/promises';
 
-
 import {
   PageGrant, getIdForRef, getIdStringForRef, isPopulated, type IUserHasId,
 } from '@growi/core';
 import { isGrobPatternPath } from '@growi/core/dist/utils/page-path-utils';
 import escapeStringRegexp from 'escape-string-regexp';
+import createError from 'http-errors';
 import mongoose, { type HydratedDocument, type Types } from 'mongoose';
 import { type OpenAI, toFile } from 'openai';
 
@@ -575,7 +575,7 @@ class OpenaiService implements IOpenaiService {
   async updateAiAssistant(aiAssistantId: string, data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument> {
     const aiAssistant = await AiAssistantModel.findOne({ owner: data.owner, _id: aiAssistantId });
     if (aiAssistant == null) {
-      throw new Error('AiAssistant document does not exist');
+      throw createError(404, 'AiAssistant document does not exist');
     }
 
     await this.validateGrantedUserGroupsForAiAssistant(
@@ -662,7 +662,7 @@ class OpenaiService implements IOpenaiService {
   async deleteAiAssistant(ownerId: string, aiAssistantId: string): Promise<AiAssistantDocument> {
     const aiAssistant = await AiAssistantModel.findOne({ owner: ownerId, _id: aiAssistantId });
     if (aiAssistant == null) {
-      throw new Error('AiAssistant document does not exist');
+      throw createError(404, 'AiAssistant document does not exist');
     }
 
     const vectorStoreRelationId = getIdStringForRef(aiAssistant.vectorStore);