UNPKG

@ace-fetch/graphql

Version:

Fetch Provider.

60 lines (59 loc) 2.14 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { isPromise, warn } from '@ace-util/core'; import { debug } from '../env'; var defaultOptions = { handler: function (error) { warn(!debug, "Error is catched by default handler, Error message: ".concat(error instanceof Error ? error.message : error)); return Promise.reject(error); }, }; function catchErrorHandler(error, config, handler) { if (!!(config === null || config === void 0 ? void 0 : config.catchError)) { return handler === null || handler === void 0 ? void 0 : handler(error); } return Promise.reject(error); } /** * regist catch error plugin on current promise request * @param request request promise * @param options catch error options */ export function registCatchError(request, options) { if (options === void 0) { options = {}; } var curOptions = __assign(__assign({}, defaultOptions), options); return function (config) { var requestPromise = request(config); if (!isPromise(requestPromise)) { return requestPromise; } return requestPromise.catch(function (error) { // TIP: catch 参捕获到 then 中抛出的异常 return catchErrorHandler(error, config, curOptions.handler); }); }; } /** * 注册异常处理插件 * 只在regist graphqls上运行 (and 自定义条件下) * @param options 插件配置 */ export var createCatchErrorPlugin = function (options) { if (options === void 0) { options = {}; } return function (_a) { var registGraphqls = _a.registGraphqls; return Object.keys(registGraphqls).reduce(function (prev, key) { prev[key] = registCatchError(registGraphqls[key], options); return prev; }, {}); }; };