Skip to content

Commit

Permalink
feat: add support translate for prisma error.
Browse files Browse the repository at this point in the history
  • Loading branch information
xufeixiang committed May 31, 2024
1 parent e35832e commit c352c92
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/common/configs/global_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func init() {
if appearance := data.Appearance; appearance != nil {
i18n.SwitchErrcodeLocale(appearance.Language)
i18n.SwitchDirectiveLocale(appearance.Language)
i18n.SwitchPrismaErrorLocale(appearance.Language)
}

if nodeLogger := data.NodeOptions.Logger; nodeLogger != nil {
Expand All @@ -90,6 +91,7 @@ func init() {
language := string(detail.Target)
i18n.SwitchErrcodeLocale(language)
i18n.SwitchDirectiveLocale(language)
i18n.SwitchPrismaErrorLocale(language)
}

if _, ok := modifies.GetModifyDetail("nodeOptions.logger.level"); ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/configs/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGenerateEnterpriseCode(t *testing.T) {
}

func generateUserLicense(licenseType consts.LicenseType, months int, userLimits map[string]int) {
requiredUserCode := "d70f6e1d2083fd88739f81ba87030339"
requiredUserCode := "1ab53c25d9622a455422cb37cf58e858"
encodeCode := utils.GenerateLicenseKey(requiredUserCode, &userLicense{
Type: licenseType,
ExpireTime: time.Now().AddDate(0, months, 0),
Expand Down
73 changes: 73 additions & 0 deletions pkg/plugins/i18n/prisma_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Package i18n
/*
查询引擎错误国际化配置,使用i18n-stringer实现
*/
package i18n

import (
"github.com/spf13/cast"
"github.com/wundergraph/wundergraph/pkg/datasources/database"
)

// First check
//go:generate $GOPATH/bin/i18n-stringer -type PrismaError -tomlpath prisma_error -check

// Second generation
//go:generate $GOPATH/bin/i18n-stringer -type PrismaError -tomlpath prisma_error -defaultlocale zh_cn

func init() {
database.TranslateErrorFunc = func(code string) string {
return PrismaError._transOne(PrismaError(cast.ToInt(code[1:])), _PrismaError_defaultLocale)
}
}

func SwitchPrismaErrorLocale(locale string) bool {
result := _PrismaError_isLocaleSupport(locale)
if result {
_PrismaError_defaultLocale = locale
}
return result
}

type PrismaError uint16

// PrismaQueryError
const (
PrismaError_P2000 PrismaError = 2000 + iota
PrismaError_P2001
PrismaError_P2002
PrismaError_P2003
PrismaError_P2004
PrismaError_P2005
PrismaError_P2006
PrismaError_P2007
PrismaError_P2008
PrismaError_P2009
PrismaError_P2010
PrismaError_P2011
PrismaError_P2012
PrismaError_P2013
PrismaError_P2014
PrismaError_P2015
PrismaError_P2016
PrismaError_P2017
PrismaError_P2018
PrismaError_P2019
PrismaError_P2020
PrismaError_P2021
PrismaError_P2022
PrismaError_P2023
PrismaError_P2024
PrismaError_P2025
PrismaError_P2026
PrismaError_P2027
PrismaError_P2028
PrismaError_P2029
PrismaError_P2030
PrismaError_P2031
PrismaError_P2033
PrismaError_P2034
PrismaError_P2035
PrismaError_P2036
PrismaError_P2037
)
37 changes: 37 additions & 0 deletions pkg/plugins/i18n/prisma_error/zh_cn/query_engine.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
PrismaError_P2000 = "列{column_name}提供的值太长"
PrismaError_P2001 = "在where条件({model_name}.{argument_name} = {argument_value})中搜索的记录不存在"
PrismaError_P2002 = "约束{target}出现重复数据"
PrismaError_P2003 = "字段{field_name}上的外键约束失败"
PrismaError_P2004 = "数据库上的约束失败: {database_error}"
PrismaError_P2005 = "存储在数据库中字段{field_name}的值{field_value}对字段类型无效"
PrismaError_P2006 = "为{model_name}字段{field_name}提供的值{field_value}无效"
PrismaError_P2007 = "数据验证错误: {database_error}"
PrismaError_P2008 = "未能在{query_position}解析查询: {query_parsing_error}"
PrismaError_P2009 = "无法在{query_position}验证查询: {query_validation_error}"
PrismaError_P2010 = "原始查询失败。代码: {code}。消息: {message}"
PrismaError_P2011 = "{constraint}上的空约束违规"
PrismaError_P2012 = "在{path}缺少一个必需值"
PrismaError_P2013 = "缺少{object_name}上字段{field_name}所需的参数{argument_name}"
PrismaError_P2014 = "您试图进行的更改将违反{model_a_name}和{model_b_name}模型之间的必要关系'{relation_name}'"
PrismaError_P2015 = "找不到相关记录。{details}"
PrismaError_P2016 = "查询解释错误。{details}"
PrismaError_P2017 = "{parent_name}和{child_name}模型之间的关系{relation_name}的记录没有连接"
PrismaError_P2018 = "找不到所需的连接记录。{details}"
PrismaError_P2019 = "输入错误。{details}"
PrismaError_P2020 = "该类型的值超出范围。{details}"
PrismaError_P2021 = "当前数据库中不存在表{table}"
PrismaError_P2022 = "当前数据库中不存在{column}列"
PrismaError_P2023 = "不一致的列数据: {message}"
PrismaError_P2024 = "从连接池中获取新连接的时已过(当前连接池超时: {timeout},连接限制: {connection_limit})。(更多信息: http://pris.ly/d/connection-pool)"
PrismaError_P2025 = "依赖一个或多个的记录找到。{cause}"
PrismaError_P2026 = "当前数据库提供程序不支持查询使用的功能: {feature}"
PrismaError_P2027 = "查询执行期间数据库出现多个错误: {errors}"
PrismaError_P2028 = "事务API错误: {error}"
PrismaError_P2029 = "查询参数限制超过错误: {message}"
PrismaError_P2030 = "找不到用于搜索的全文索引,请尝试在您的模式中添加@@fulltext([Fields...])"
PrismaError_P2031 = "Prisma需要执行事务,这需要您的MongoDB服务器作为副本集运行。查看详情: https://pris.ly/d/mongodb-replica-set"
PrismaError_P2033 = "查询中使用的数字不适合64位有符号整数。如果您试图存储大整数,请考虑使用BigInt作为字段类型"
PrismaError_P2034 = "由于写入冲突或死锁,事务失败。请重试您的事务"
PrismaError_P2035 = "数据库上的断言违规: {database_error}"
PrismaError_P2036 = "外部连接器错误(id {id})"
PrismaError_P2037 = "打开的数据库连接太多: {message}"
240 changes: 240 additions & 0 deletions pkg/plugins/i18n/prismaerror_i18n_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c352c92

Please sign in to comment.