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, 828: 1.81 / 2,
}, },
sourceRoot: 'src', sourceRoot: 'src',
outputRoot: 'dist', outputRoot: `dist/${process.env.TARO_ENV}`,
plugins: ['@tarojs/plugin-html', '@tarojs/plugin-platform-alipay-dd'], plugins: ['@tarojs/plugin-html', '@tarojs/plugin-platform-alipay-dd'],
defineConstants: {}, defineConstants: {},
copy: { copy: {

View File

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

View File

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

View File

@ -7,18 +7,28 @@ const config = ref<string>('');
const response = ref<string>(''); const response = ref<string>('');
const error = 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.baseURL = 'https://jsonplaceholder.typicode.com';
axios.defaults.errorHandler = (err) => { axios.defaults.errorHandler = (err) => {
console.log('[debug]', (err as any).response);
error.value = `<pre>${JSON.stringify(err, null, 2)}</pre>`; error.value = `<pre>${JSON.stringify(err, null, 2)}</pre>`;
Taro.hideLoading(); Taro.hideLoading();
Taro.showToast({ Taro.showToast({
icon: 'none', icon: 'none',
title: (err as any).response.data.errMsg, title: (err as any).response.data?.errMsg ?? '未知错误',
}); });
return Promise.reject(err); return Promise.reject(err);
}; };
axios.use(async (ctx, next) => { 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>`; config.value = `<pre>${JSON.stringify(ctx.req, null, 2)}</pre>`;
error.value = ''; error.value = '';
response.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({ defineExpose({
getRequest, getRequest,
postRequest, postRequest,
@ -105,6 +161,18 @@ defineExpose({
<button class="button" type="primary" @click="deleteRequest"> <button class="button" type="primary" @click="deleteRequest">
DELETE 请求 DELETE 请求
</button> </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: config:
<view class="code" v-html="config"></view> <view class="code" v-html="config"></view>

View File

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