From 046d6d72300b0e1a047a0343960e7649fdcb687d Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 27 Oct 2020 00:56:12 +0800 Subject: [PATCH] show friendly error when user registration is disabled --- pkg/utils/api.go | 1 + src/lib/i18n.js | 12 ++++++++++++ src/locales/en.js | 2 ++ src/locales/zh_Hans.js | 2 ++ 4 files changed, 17 insertions(+) diff --git a/pkg/utils/api.go b/pkg/utils/api.go index 34db4712..46308bd0 100644 --- a/pkg/utils/api.go +++ b/pkg/utils/api.go @@ -37,6 +37,7 @@ func PrintErrorResult(c *core.Context, err *errs.Error) { "success": false, "errorCode": err.Code(), "errorMessage": errorMessage, + "path": c.Request.URL.Path, }) } diff --git a/src/lib/i18n.js b/src/lib/i18n.js index 04a825e0..1f3bd102 100644 --- a/src/lib/i18n.js +++ b/src/lib/i18n.js @@ -1,5 +1,11 @@ import { defaultLanguage, allLanguages } from '../locales/index.js' +const apiNotFoundErrorCode = 100001; +const specifiedApiNotFoundErrors = { + '/api/register.json': { + message: 'User registration is disabled' + } +}; const validatorErrorCode = 200000; const parameterizedErrors = [ { @@ -134,6 +140,12 @@ export function getI18nOptions() { } export function getLocalizedError(error) { + if (error.errorCode === apiNotFoundErrorCode && specifiedApiNotFoundErrors[error.path]) { + return { + message: `${specifiedApiNotFoundErrors[error.path].message}` + }; + } + if (error.errorCode !== validatorErrorCode) { return { message: `error.${error.errorMessage}` diff --git a/src/locales/en.js b/src/locales/en.js index 98d1f7ee..ea9e8fca 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -6,6 +6,7 @@ export default { }, 'error': { 'system error': 'System Error', + 'api not found': 'Failed to request api', 'incomplete or incorrect submission': 'Incomplete or incorrect submission', 'operation failed': 'Operation failed', 'nothing will be updated': 'Nothing will be updated', @@ -92,6 +93,7 @@ export default { 'Language': 'Language', 'You have been successfully registered': 'You have been successfully registered', 'Unable to sign up': 'Unable to sign up', + 'User registration is disabled': 'User registration is disabled', 'Log Out': 'Log Out', 'Are you sure you want to log out?': 'Are you sure you want to log out?', 'Unable to logout': 'Unable to logout', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index ffabef9c..4ad9518b 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -6,6 +6,7 @@ export default { }, 'error': { 'system error': '系统错误', + 'api not found': '接口调用失败', 'incomplete or incorrect submission': '提交不完整或不正确', 'operation failed': '操作失败', 'nothing will be updated': '没有内容更新', @@ -92,6 +93,7 @@ export default { 'Language': '语言', 'You have been successfully registered': '注册成功', 'Unable to sign up': '无法注册', + 'User registration is disabled': '用户注册已禁用', 'Log Out': '退出登录', 'Are you sure you want to log out?': '您确定是否要退出登录?', 'Unable to logout': '无法退出登录',