fix(server): 处理错误消息
This commit is contained in:
@@ -4,7 +4,7 @@ import type {Request as ExpressRequest} from 'express';
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import type {UserInfo} from "../types/user";
|
import type {UserInfo} from "../types/user";
|
||||||
|
|
||||||
export const getGroupUsers = async (groupId: number, token: string): Promise<UserInfo[]> => {
|
export const getGroupMembers = async (groupId: number, token: string): Promise<UserInfo[]> => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post<ApiResponse<UserInfo[]>>(
|
const response = await axios.post<ApiResponse<UserInfo[]>>(
|
||||||
"https://egret.byteawake.com/api/group/members",
|
"https://egret.byteawake.com/api/group/members",
|
||||||
@@ -18,20 +18,20 @@ export const getGroupUsers = async (groupId: number, token: string): Promise<Use
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (response.data.code !== 200) {
|
if (response.data.code !== 200) {
|
||||||
throw new Error(`Failed to get group users: ${response.data.message}`);
|
throw new Error(response.data.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.data.data ?? [];
|
return response.data.data ?? [];
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
// API returned error response
|
// API returned error response
|
||||||
throw new ApiError(400, `Failed to get user information: ${error.response.status} ${error.response.statusText}`);
|
throw new ApiError(400, `Failed to get group members information: ${error.response.status} ${error.response.statusText}`);
|
||||||
} else if (error.request) {
|
} else if (error.request) {
|
||||||
// Request was sent but no response received
|
// Request was sent but no response received
|
||||||
throw new ApiError(400, "Failed to get user information: timeout or network error");
|
throw new ApiError(400, "Failed to get group members information: timeout or network error");
|
||||||
} else {
|
} else {
|
||||||
// Other errors
|
// Other errors
|
||||||
throw new ApiError(400, `Failed to get user information: ${error.message}`);
|
throw new ApiError(400, `Failed to get group members information: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -49,7 +49,7 @@ export class GroupController extends Controller {
|
|||||||
@Response<ApiResponse<UserInfo[]>>(200, 'Success')
|
@Response<ApiResponse<UserInfo[]>>(200, 'Success')
|
||||||
@Response<ApiResponse<null>>(400, 'Bad Request')
|
@Response<ApiResponse<null>>(400, 'Bad Request')
|
||||||
@Response<ApiResponse<null>>(401, 'Unauthorized')
|
@Response<ApiResponse<null>>(401, 'Unauthorized')
|
||||||
public async getGroupUsers(@Request() req: ExpressRequest): Promise<ApiResponse<UserInfo[]>> {
|
public async getGroupMembers(@Request() req: ExpressRequest): Promise<ApiResponse<UserInfo[]>> {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
const groupId = req.headers.groupid;
|
const groupId = req.headers.groupid;
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ export class GroupController extends Controller {
|
|||||||
throw new ApiError(400, 'groupId is required in headers');
|
throw new ApiError(400, 'groupId is required in headers');
|
||||||
}
|
}
|
||||||
|
|
||||||
const users = await getGroupUsers(Number(groupId), user.token);
|
const users = await getGroupMembers(Number(groupId), user.token);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: 200,
|
code: 200,
|
||||||
|
|||||||
Reference in New Issue
Block a user