|
@@ -1,17 +1,10 @@
|
|
|
import { App, Config, Inject, InjectClient, Provide } from '@midwayjs/core';
|
|
|
import { Application, Context } from '@midwayjs/koa';
|
|
|
import { RequestBase } from '../interface/proxy.interface';
|
|
|
-import {
|
|
|
- get,
|
|
|
- isEqual,
|
|
|
- isObject,
|
|
|
- isString,
|
|
|
- lowerCase,
|
|
|
- omit,
|
|
|
- pick,
|
|
|
-} from 'lodash';
|
|
|
+import { get, lowerCase, omit, pick } from 'lodash';
|
|
|
import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
|
|
|
import { PemService } from './pem.service';
|
|
|
+import { ServiceError } from '../error/service.error';
|
|
|
@Provide()
|
|
|
export class ProxyService {
|
|
|
@Inject()
|
|
@@ -103,18 +96,33 @@ export class ProxyService {
|
|
|
}
|
|
|
}
|
|
|
reqConfig = JSON.parse(JSON.stringify(reqConfig));
|
|
|
- const res = await this.serviceAxios.request(reqConfig);
|
|
|
- if (res.status !== 200) throw new Error('proxy service request error');
|
|
|
- const result = res.data;
|
|
|
- if (this.useCrypto) {
|
|
|
- // 需要将返回的数据加密成字符串
|
|
|
- if (get(result, 'data')) {
|
|
|
- const enReturn = this.pemService.encrypt(
|
|
|
- omit(res.data, ['errcode', 'errmsg'])
|
|
|
- );
|
|
|
- result.data = enReturn;
|
|
|
+ let result;
|
|
|
+ try {
|
|
|
+ const res = await this.serviceAxios.request(reqConfig);
|
|
|
+ if (res.status !== 200) throw new Error('proxy service request error');
|
|
|
+ result = res.data;
|
|
|
+ } catch (error) {
|
|
|
+ // 请求不是自定义的错误,把错误隐藏,写到日志中
|
|
|
+ this.ctx.logger.error(error.stack);
|
|
|
+ return {
|
|
|
+ errmsg: 'proxy service request error',
|
|
|
+ code: get(error, 'response.status'),
|
|
|
+ };
|
|
|
+ }
|
|
|
+ if (result.errcode === 0) {
|
|
|
+ if (this.useCrypto) {
|
|
|
+ // 需要将返回的数据加密成字符串
|
|
|
+ if (get(result, 'data')) {
|
|
|
+ const enReturn = this.pemService.encrypt(
|
|
|
+ omit(result, ['errcode', 'errmsg'])
|
|
|
+ );
|
|
|
+ result.data = enReturn;
|
|
|
+ }
|
|
|
}
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ // 需要将异常信息翻译并返回
|
|
|
+ throw new ServiceError(result.errcode);
|
|
|
}
|
|
|
- return result;
|
|
|
}
|
|
|
}
|