fix: 修复 errno 和 errMsg 丢失

pull/49/head
zjx0905 2023-08-13 14:04:06 +08:00
parent 391b47d7a9
commit 71e3007389
5 changed files with 137 additions and 38 deletions

View File

@ -18,7 +18,7 @@ const config = {
828: 1.81 / 2,
},
sourceRoot: 'src',
outputRoot: 'dist',
outputRoot: `dist/${process.env.TARO_ENV}`,
plugins: ['@tarojs/plugin-html', '@tarojs/plugin-platform-alipay-dd'],
defineConstants: {},
copy: {

View File

@ -11,6 +11,8 @@
"build:qq": "taro build --type qq",
"build:jd": "taro build --type jd",
"build:dd": "taro build --type dd",
"build:lark": "taro build --type lark",
"build:kwai": "taro build --type kwai",
"build:quickapp": "taro build --type quickapp",
"dev:weapp": "npm run build:weapp -- --watch",
"dev:swan": "npm run build:swan -- --watch",
@ -21,6 +23,8 @@
"dev:qq": "npm run build:qq -- --watch",
"dev:jd": "npm run build:jd -- --watch",
"dev:dd": "npm run build:dd -- --watch",
"dev:lark": "npm run build:lark -- --watch",
"dev:kwai": "npm run build:kwai -- --watch",
"dev:quickapp": "npm run build:quickapp -- --watch"
},
"dependencies": {

View File

@ -1,7 +1,5 @@
{
"miniprogramRoot": "./dist",
"projectname": "axios-miniprogram",
"appid": "wx48e277051b32f95b",
"setting": {
"urlCheck": false,
"es6": false,

View File

@ -7,18 +7,28 @@ const config = ref<string>('');
const response = ref<string>('');
const error = ref<string>('');
axios.defaults.adapter = axios.createAdapter({
request: Taro.request as any,
download: Taro.downloadFile as any,
upload: Taro.uploadFile as any,
});
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com';
axios.defaults.errorHandler = (err) => {
console.log('[debug]', (err as any).response);
error.value = `<pre>${JSON.stringify(err, null, 2)}</pre>`;
Taro.hideLoading();
Taro.showToast({
icon: 'none',
title: (err as any).response.data.errMsg,
title: (err as any).response.data?.errMsg ?? '未知错误',
});
return Promise.reject(err);
};
axios.use(async (ctx, next) => {
Taro.showLoading();
console.log('[debug]', ctx);
Taro.showLoading({
title: 'Loading...',
});
config.value = `<pre>${JSON.stringify(ctx.req, null, 2)}</pre>`;
error.value = '';
response.value = '';
@ -84,6 +94,52 @@ function deleteRequest() {
});
}
function downloadRequest() {
axios.get(
'/users/:id',
{
id: 1,
},
{
download: true,
},
);
}
function uploadRequest() {
Taro.chooseImage({
count: 1,
success({ tempFilePaths }) {
axios.post(
'/users',
{
name: 'filename',
filePath: tempFilePaths[0],
},
{
upload: true,
},
);
},
});
}
function errorRequest() {
axios.get('/users/:id', {
id: Infinity,
});
}
function failRequest() {
axios.post(
'/users',
{},
{
upload: true,
},
);
}
defineExpose({
getRequest,
postRequest,
@ -105,6 +161,18 @@ defineExpose({
<button class="button" type="primary" @click="deleteRequest">
DELETE 请求
</button>
<button class="button" type="primary" @click="downloadRequest">
DOWNLOAD 请求
</button>
<button class="button" type="primary" @click="uploadRequest">
UPLOAD 请求
</button>
<button class="button" type="primary" @click="errorRequest">
ERROR 请求
</button>
<button class="button" type="primary" @click="failRequest">
FAIL 请求
</button>
config:
<view class="code" v-html="config"></view>

View File

@ -1,6 +1,6 @@
import { isFunction, isPlainObject } from '../helpers/types';
import { assert } from '../helpers/error';
import { orgIgnore } from '../helpers/ignore';
import { ignore, orgIgnore } from '../helpers/ignore';
import {
AxiosProgressEvent,
AxiosRequestFormData,
@ -170,11 +170,11 @@ export interface AxiosAdapterUploadOptions
extends AxiosAdapterBaseOptions,
AxiosRequestFormData {
/**
* [fileName name](https://open.dingtalk.com/document/orgapp/dd-upload-objects#title-ngk-rr1-eow)
* [fileName name](https://open.dingtalk.com/document/orgapp/dd-upload-objects#title-ngk-rr1-eow)
*/
fileName: string;
/**
* |
* |
*/
fileType?: 'image' | 'video' | 'audie';
/**
@ -272,38 +272,63 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
return {
...config,
header: config.headers,
success(response: AxiosAdapterResponse) {
transformResponse(response);
success(_response: AxiosAdapterResponse) {
const response = transformResponse(_response) as AxiosAdapterResponse;
config.success(response);
},
fail(responseError: AxiosAdapterResponseError) {
responseError.data = {
errMsg: responseError.errMsg ?? responseError.errorMessage,
errno: responseError.errno ?? responseError.error,
};
fail(_responseError: AxiosAdapterResponseError) {
const responseError = Object.assign(transformResponse(_responseError), {
data: {
errno:
// 微信 | 飞书新规范
_responseError.errno ??
// 支付宝 | 钉钉
_responseError.error ??
// 百度 | 360 | 飞书
_responseError.errCode ??
// 抖音
_responseError.errNo,
errMsg:
// 飞书新规范
_responseError.errString ??
// 微信 | 支付宝 | 百度 | 抖音 | QQ | 360 | 飞书
_responseError.errMsg ??
// 钉钉
_responseError.errorMessage,
},
});
transformResponse(responseError);
config.fail(responseError);
},
};
}
function transformResponse(
response: AxiosAdapterResponse | AxiosAdapterResponseError,
_response: AxiosAdapterResponse | AxiosAdapterResponseError,
) {
response.status = response.status ?? response.statusCode;
response.headers = response.headers ?? response.header;
return Object.assign(
ignore(
_response,
'statusCode',
'header',
orgIgnore(response, [
'statusCode',
'errMsg',
'errno',
'header',
// 钉钉小程序
'error',
// 钉钉小程序
'errorMessage',
]);
// 错误码
'errno',
'error',
'errCode',
'errNo',
// 错误消息
'errMsg',
'errorMessage',
'errString',
),
{
status: _response.status ?? _response.statusCode,
headers: _response.headers ?? _response.header,
},
);
}
function processRequest(
@ -336,16 +361,20 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
const options = baseOptions as AxiosAdapterDownloadOptions;
const { params, success } = options;
options.filePath = params?.filePath;
options.success = (response) => {
response.data = {
filePath: response.filePath,
tempFilePath:
response.tempFilePath ??
// response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。
response.apFilePath,
};
options.success = (_response) => {
const response = Object.assign(
ignore(_response, 'tempFilePath', 'apFilePath', 'filePath'),
{
data: {
filePath: _response.filePath,
tempFilePath:
_response.tempFilePath ??
// response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。
_response.apFilePath,
},
},
);
orgIgnore(response, ['tempFilePath', 'apFilePath', 'filePath']);
success(response);
};