Skip to content

Commit

Permalink
fix: improve some error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Jul 13, 2024
1 parent 8038260 commit 8326dd8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub enum Error {
#[error(transparent)]
Db(#[from] sea_orm::error::DbErr),

#[error(transparent)]
SerdeJson(#[from] serde_json::Error),

#[error(transparent)]
Tauri(#[from] tauri::Error),

Expand Down Expand Up @@ -54,6 +51,9 @@ pub enum Error {
GachaRecordFetcherChannelJoin,

// UIGF & SRGF
#[error("{0}")]
UIGFOrSRGFSerdeJson(serde_json::Error),

#[error("UIGF or SRGF Mismatched UID: expected {expected:?}, actual {actual:?}")]
UIGFOrSRGFMismatchedUID { expected: String, actual: String },

Expand Down Expand Up @@ -95,6 +95,7 @@ impl_error_identifiers! {
GachaRecordRetcode => GACHA_RECORD_RETCODE,
GachaRecordFetcherChannelSend => GACHA_RECORD_FETCHER_CHANNEL_SEND,
GachaRecordFetcherChannelJoin => GACHA_RECORD_FETCHER_CHANNEL_JOIN,
UIGFOrSRGFSerdeJson => UIGF_OR_SRGF_SERDE_JSON,
UIGFOrSRGFMismatchedUID => UIGF_OR_SRGF_MISMATCHED_UID,
UIGFOrSRGFInvalidField => UIGF_OR_SRGF_INVALID_FIELD,
AccountAlreadyExists => ACCOUNT_ALREADY_EXISTS,
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/gacha/srgf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ impl SRGF {
}

pub fn from_reader(reader: impl Read) -> Result<Self> {
Ok(serde_json::from_reader(reader)?)
serde_json::from_reader(reader).map_err(Error::UIGFOrSRGFSerdeJson)
}

pub fn to_writer(&self, writer: impl Write, pretty: bool) -> Result<()> {
if pretty {
Ok(serde_json::to_writer_pretty(writer, self)?)
serde_json::to_writer_pretty(writer, self).map_err(Error::UIGFOrSRGFSerdeJson)
} else {
Ok(serde_json::to_writer(writer, self)?)
serde_json::to_writer(writer, self).map_err(Error::UIGFOrSRGFSerdeJson)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/gacha/uigf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ impl UIGF {
}

pub fn from_reader(reader: impl Read) -> Result<Self> {
Ok(serde_json::from_reader(reader)?)
serde_json::from_reader(reader).map_err(Error::UIGFOrSRGFSerdeJson)
}

pub fn to_writer(&self, writer: impl Write, pretty: bool) -> Result<()> {
if pretty {
Ok(serde_json::to_writer_pretty(writer, self)?)
serde_json::to_writer_pretty(writer, self).map_err(Error::UIGFOrSRGFSerdeJson)
} else {
Ok(serde_json::to_writer(writer, self)?)
serde_json::to_writer(writer, self).map_err(Error::UIGFOrSRGFSerdeJson)
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions src/components/gacha/GachaLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ export default function GachaLayout () {
if (error && (error instanceof Error || typeof error === 'object')) {
const msg = (error as { message: string }).message
const identifier = (error as { identifier?: string }).identifier
let knownMessage = identifier ? KnownErrorIdentifiers[identifier] : undefined
if (knownMessage) {
const knownMessage = identifier ? KnownErrorIdentifiers[identifier] : undefined
const appendMessage = Array.isArray(knownMessage)
let pretty = appendMessage ? knownMessage[0] : knownMessage
if (pretty && appendMessage) {
let index: number
if ((index = msg.indexOf(':')) !== -1) {
knownMessage += msg.substring(index + 1)
}
if (identifier === 'INTERNAL_CRATE') {
knownMessage += msg
pretty += msg.substring(index)
} else if (appendMessage) {
pretty += msg
}
}
message = knownMessage || msg

message = pretty || msg
} else if (error) {
message = String(error)
}
Expand Down Expand Up @@ -119,13 +121,14 @@ export default function GachaLayout () {
)
}

const KnownErrorIdentifiers: Record<string, string> = {
INTERNAL_CRATE: '内部错误:',
WEB_CACHES: '读取硬盘缓存失败:未找到有效文件夹!请检查账号的游戏数据文件夹!',
const KnownErrorIdentifiers: Record<string, string | [string]> = {
INTERNAL_CRATE: ['内部错误'],
WEB_CACHES: '读取硬盘缓存失败:未找到有效文件夹!请检查账号的游戏数据文件夹是否正确!',
ILLEGAL_GACHA_URL: '无效的抽卡链接!',
VACANT_GACHA_URL: '未找到有效的抽卡链接。请尝试在游戏内打开抽卡历史记录界面!',
TIMEOUTD_GACHA_URL: '抽卡链接已经过期失效。请重新在游戏内打开抽卡历史记录界面!',
VISIT_TOO_FREQUENTLY_GACHA_URL: '请求获取抽卡记录 API 速率过快!请稍等几秒后再次重试!',
UIGF_OR_SRGF_MISMATCHED_UID: '待导入的 UIGF 或 SRGF 数据 UID 与当前账号不匹配!',
UIGF_OR_SRGF_INVALID_FIELD: '待导入的 UIGF 或 SRGF 数据中存在无效的字段!'
UIGF_OR_SRGF_SERDE_JSON: ['待导入的 UIGF 或 SRGF 数据文件格式不正确'],
UIGF_OR_SRGF_MISMATCHED_UID: ['待导入的 UIGF 或 SRGF 数据 UID 与当前账号不匹配'],
UIGF_OR_SRGF_INVALID_FIELD: ['待导入的 UIGF 或 SRGF 数据中存在无效的字段']
}

0 comments on commit 8326dd8

Please sign in to comment.