Skip to content

Commit

Permalink
feat(axios): error notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisGray0626 committed Jul 23, 2024
1 parent 2de0261 commit d7e8f98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/api/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/

import axios, {AxiosInstance} from "axios";
import {ElNotification} from 'element-plus'
import {SUCCESS_CODE} from "@/constant";

export interface AxiosInstanceConfig {
baseURL: string;
timeout?: number;
}
// 请求头设置
// instance.defaults.headers.post["Content-Type"] = "application/json";
// 拦截器设置
function setInterceptor(instance: AxiosInstance) {
// 请求拦截器设置
Expand All @@ -27,9 +27,28 @@ function setInterceptor(instance: AxiosInstance) {
// 响应拦截器设置
instance.interceptors.response.use(
(response) => {
// 检查接口调用状态
if (response.data.code != SUCCESS_CODE) {
// 弹出错误信息
ElNotification.error({
title: 'Error',
message: response.data.message,
duration: 2000,
})
}
console.error(response.data)

return response;
},
(error) => {
// 弹出错误信息
ElNotification.error({
title: 'Error',
message: response.data.message,

Check failure on line 47 in src/api/axios.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.2)

Cannot find name 'response'. Did you mean 'Response'?
duration: 2000,
})
console.error(response.data)

Check failure on line 50 in src/api/axios.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.2)

Cannot find name 'response'. Did you mean 'Response'?

return Promise.reject(error);
},
);
Expand Down
2 changes: 2 additions & 0 deletions src/constant/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const SUCCESS_CODE = 70200;

export const JOB_EXAMPLE = {
task: "Calculation of Flood Affected Area",
subtasks: [
Expand Down

0 comments on commit d7e8f98

Please sign in to comment.