diff --git a/src/main/translate/Api.ts b/src/main/translate/Api.ts index 709bf94..93d9c54 100644 --- a/src/main/translate/Api.ts +++ b/src/main/translate/Api.ts @@ -116,7 +116,11 @@ export default class Api implements yuki.Translator { const scriptString = this.config.responseBodyPattern .substring(1) .replace('%RESPONSE%', `result = response`) - vm.runInNewContext(scriptString, this.responseVmContext) + try { + vm.runInNewContext(scriptString, this.responseVmContext) + } catch (e) { + return `ERR: ${e}` + } return this.responseVmContext.result } diff --git a/test/unit/specs/main/Api.spec.ts b/test/unit/specs/main/Api.spec.ts index a723430..a369f97 100644 --- a/test/unit/specs/main/Api.spec.ts +++ b/test/unit/specs/main/Api.spec.ts @@ -59,7 +59,7 @@ describe('Api', () => { expect(translations.translations.googleCN).to.equal('如果捕获了尤马坤,则为210日元。我知道了 ...') expect(translations.translations.caiyun).to.be.oneOf([ '攻下悠真的话是210日元吗。 原来如此', - undefined + "ERR: TypeError: Cannot read property 'target' of undefined" ]) } catch (e) { return done(e)