Skip to content

Commit 2db8292

Browse files
committed
feat: optimize error handler
1 parent 5741868 commit 2db8292

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/services/codingServer.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,9 @@ export default class CodingServer {
5454
},
5555
});
5656

57-
if (result.code) {
58-
toast.error(result.msg);
59-
return Promise.reject(result.msg);
60-
}
61-
6257
return result?.data;
6358
} catch (err) {
64-
throw new Error(err);
59+
console.error(err);
6560
}
6661
}
6762

@@ -82,7 +77,7 @@ export default class CodingServer {
8277
});
8378
return result?.data?.list || [];
8479
} catch (err) {
85-
throw new Error(err);
80+
console.error(err);
8681
}
8782
}
8883

@@ -97,19 +92,35 @@ export default class CodingServer {
9792
},
9893
});
9994

100-
if (result.code) {
101-
toast.error(result.msg);
102-
return Promise.reject(result.msg);
103-
}
104-
10595
return result?.data?.depots || [];
10696
} catch (err) {
107-
throw new Error(err);
97+
console.error(err);
10898
}
10999
}
110100

111-
async createDepot(team: string = this._repo?.team, project: string = this._repo?.project, depot: string) {
101+
async createProject(team: string, project: string) {
112102
try {
103+
const result = await axios({
104+
method: 'post',
105+
url: `https://${team}.coding.net/api/team/${team}/template-project?access_token=${this._session.accessToken}`,
106+
data: {
107+
name: project,
108+
displayName: project,
109+
projectTemplate: 'DEV_OPS',
110+
icon: '/static/project_icon/scenery-version-2-5.svg',
111+
},
112+
});
113+
114+
return result.data;
115+
} catch (err) {
116+
console.error(err);
117+
}
118+
}
119+
120+
async createDepot(team: string, project: string, depot: string) {
121+
try {
122+
await this.createProject(team, project);
123+
113124
const result = await axios({
114125
method: 'post',
115126
url: `https://${team}.coding.net/api/user/${team}/project/${project}/depot?access_token=${this._session.accessToken}`,
@@ -124,7 +135,7 @@ export default class CodingServer {
124135
}),
125136
});
126137
} catch (err) {
127-
throw new Error(err);
138+
console.error(err);
128139
}
129140
}
130141
}

src/utils/axios.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import axios, { AxiosRequestConfig, AxiosInstance } from 'axios';
2+
import toast from './toast';
3+
4+
const formatErrorMessage = (msg: string | Record<string, string>) => {
5+
if (typeof msg === 'string') return msg;
6+
return Object.values(msg).join();
7+
};
28

39
const handleResponse = (response: any) => {
4-
return response.data;
10+
const result = response.data;
11+
12+
if (result.code) {
13+
const message = formatErrorMessage(result.msg);
14+
toast.error(message);
15+
throw new Error(message);
16+
}
17+
18+
return result;
519
};
620

721
const handleError = (error: any) => {

0 commit comments

Comments
 (0)