diff --git a/mail-worker/src/hono/hono.js b/mail-worker/src/hono/hono.js index f8c48bf..d385aa4 100644 --- a/mail-worker/src/hono/hono.js +++ b/mail-worker/src/hono/hono.js @@ -12,6 +12,19 @@ app.onError((err, c) => { }else { console.error(err); } + + if (err.message === `Cannot read properties of undefined (reading 'get')`) { + return c.text('初始化失败:KV数据库未绑定或变量名错误 Initialization failed: KV database not bound or invalid variable name'); + } + + if (err.message === `Cannot read properties of undefined (reading 'put')`) { + return c.text('初始化失败:KV数据库未绑定或变量名错误 Initialization failed: KV database not bound or invalid variable name'); + } + + if (err.message === `Cannot read properties of undefined (reading 'prepare')`) { + return c.text('初始化失败:D1数据库未绑定或变量名错误 Initialization failed: D1 database not bound or invalid variable name'); + } + return c.json(result.fail(err.message, err.code)); }); diff --git a/mail-worker/src/i18n/en.js b/mail-worker/src/i18n/en.js index 169abbc..48f157a 100644 --- a/mail-worker/src/i18n/en.js +++ b/mail-worker/src/i18n/en.js @@ -45,6 +45,7 @@ const en = { roleNameExist: 'Role name already exists', delDefRole: 'Default role cannot be deleted', notJsonDomain: 'Environment variable "domain" must be in JSON format', + noDomainVariable: 'Environment variable domain cannot be empty', noOsUpBack: 'Cannot upload background: R2 object storage not configured', noOsDomainUpBack: 'Cannot upload background: R2 domain not configured', starNotExistEmail: 'Starred email does not exist', diff --git a/mail-worker/src/i18n/zh.js b/mail-worker/src/i18n/zh.js index dae4d04..9924dc4 100644 --- a/mail-worker/src/i18n/zh.js +++ b/mail-worker/src/i18n/zh.js @@ -45,6 +45,7 @@ const zh = { roleNameExist: '身份名已存在', delDefRole: '默认身份不能删除', notJsonDomain: '环境变量domain必须是JSON类型', + noDomainVariable: '环境变量domain不能为空', noOsUpBack: 'r2对象存储未配置不能上传背景', noOsDomainUpBack: 'r2域名未配置不能上传背景', starNotExistEmail: '星标的邮件不存在', diff --git a/mail-worker/src/service/setting-service.js b/mail-worker/src/service/setting-service.js index b69ad62..c4fb661 100644 --- a/mail-worker/src/service/setting-service.js +++ b/mail-worker/src/service/setting-service.js @@ -21,8 +21,10 @@ const settingService = { }, async query(c) { + const setting = await c.env.kv.get(KvConst.SETTING, { type: 'json' }); let domainList = c.env.domain; + if (typeof domainList === 'string') { try { domainList = JSON.parse(domainList) @@ -30,6 +32,11 @@ const settingService = { throw new BizError(t('notJsonDomain')); } } + + if (!c.env.domain) { + throw new BizError(t('noDomainVariable')); + } + domainList = domainList.map(item => '@' + item); setting.domainList = domainList; return setting;