Просмотр исходного кода

♻️ refactor(user): modify error handling in user service

- remove unused HTTPException import
- replace HTTPException with standard Error in createUser method
- replace HTTPException with standard Error in getUsers method
- enhance error messages to include original error details
yourname 4 месяцев назад
Родитель
Сommit
b1242481be
1 измененных файлов с 2 добавлено и 3 удалено
  1. 2 3
      src/server/modules/users/user.service.ts

+ 2 - 3
src/server/modules/users/user.service.ts

@@ -1,4 +1,3 @@
-import { HTTPException } from 'hono/http-exception'
 import { DataSource } from 'typeorm';
 import { UserEntity as User } from './user.entity';
 import * as bcrypt from 'bcrypt';
@@ -27,7 +26,7 @@ export class UserService {
       return await this.userRepository.save(user);
     } catch (error) {
       console.error('Error creating user:', error);
-      throw new HTTPException(400,{ message: 'Failed to create user', cause: error})
+      throw new Error(`Failed to create user: ${error instanceof Error ? error.message : String(error)}`)
     }
   }
 
@@ -144,7 +143,7 @@ export class UserService {
       return users;
     } catch (error) {
       console.error('Error getting users:', error);
-      throw new HTTPException(500, { message: 'Failed to get users', cause: error })
+      throw new Error(`Failed to get users: ${error instanceof Error ? error.message : String(error)}`)
     }
   }