diff --git a/examples/components/Play.jsx b/examples/components/Play.jsx index 56cd90a02..19a6ccf1c 100644 --- a/examples/components/Play.jsx +++ b/examples/components/Play.jsx @@ -108,7 +108,7 @@ export default class PlayGround extends React.Component { router.push(to); } }, - fetcher: config => { + fetcher: async config => { config = { dataType: 'json', ...config @@ -120,7 +120,31 @@ export default class PlayGround extends React.Component { config.headers['Content-Type'] = 'application/json'; } - return axios[config.method](config.url, config.data, config); + // 支持返回各种报错信息 + config.validateStatus = function (status) { + return true; + }; + + const response = await axios[config.method]( + config.url, + config.data, + config + ); + + if (response.status >= 400) { + if (response.data) { + if (response.data.msg) { + throw new Error(response.data.msg); + } else { + throw new Error( + '接口报错:' + JSON.stringify(response.data, null, 2) + ); + } + } else { + throw new Error(`接口出错,状态码是 ${response.status}`); + } + } + return response; }, isCancel: value => axios.isCancel(value), notify: (type, msg) => diff --git a/examples/embed.tsx b/examples/embed.tsx index 626775966..d73b83dd5 100644 --- a/examples/embed.tsx +++ b/examples/embed.tsx @@ -125,7 +125,7 @@ export function embed( const responseAdaptor = (api: any) => (value: any) => { let response = value.data; // 之前拼写错了,需要兼容 - if (env.responseAdpater) { + if (env && env.responseAdpater) { env.responseAdaptor = env.responseAdpater; } if (env && env.responseAdaptor) { @@ -248,10 +248,10 @@ export function embed( location.href = to; } }, - fetcher: (api: any) => { + fetcher: async (api: any) => { let {url, method, data, responseType, config, headers} = api; config = config || {}; - config.withCredentials = true; + // config.withCredentials = true; responseType && (config.responseType = responseType); if (config.cancelExecutor) { @@ -277,10 +277,31 @@ export function embed( config.headers['Content-Type'] = 'application/json'; } + // 支持返回各种报错信息 + config.validateStatus = function (status) { + return true; + }; + data && (config.data = data); - return axios(url, config) - .then(attachmentAdpator) - .then(responseAdaptor(api)); + let response = await axios(url, config); + response = attachmentAdpator(response); + response = responseAdaptor(api)(response); + + if (response.status >= 400) { + if (response.data) { + if (response.data.msg) { + throw new Error(response.data.msg); + } else { + throw new Error( + '接口报错:' + JSON.stringify(response.data, null, 2) + ); + } + } else { + throw new Error(`接口出错,状态码是 ${response.status}`); + } + } + + return response; }, isCancel: (value: any) => (axios as any).isCancel(value), copy: (contents: string, options: any = {}) => { diff --git a/examples/mobile.tsx b/examples/mobile.tsx index cb935e7ff..c1b56b92a 100644 --- a/examples/mobile.tsx +++ b/examples/mobile.tsx @@ -32,7 +32,7 @@ class AMISComponent extends React.Component { return this.state.schema ? (