Skip to content

Commit

Permalink
feat: 优化错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Oct 21, 2024
1 parent 2a7d5fa commit 83bc1d3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
20 changes: 10 additions & 10 deletions jkit/identifier_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

def user_url_to_slug(x: str, /) -> str:
if not is_user_url(x):
raise ValueError(f"{x} 不是有效的用户链接")
raise ValueError(f"用户链接无效:{x}")

return x.replace("https://www.jianshu.com/u/", "").replace("/", "")


def user_slug_to_url(x: str, /) -> str:
if not is_user_slug(x):
raise ValueError(f"{x} 不是有效的用户 Slug")
raise ValueError(f"用户 Slug 无效:{x}")

return f"https://www.jianshu.com/u/{x}"

Expand All @@ -40,55 +40,55 @@ async def user_slug_to_id(x: str, /) -> int:

def article_url_to_slug(x: str, /) -> str:
if not is_article_url(x):
raise ValueError(f"{x} 不是有效的文章链接")
raise ValueError(f"文章链接无效:{x}")

return x.replace("https://www.jianshu.com/p/", "").replace("/", "")


def article_slug_to_url(x: str, /) -> str:
if not is_article_slug(x):
raise ValueError(f"{x} 不是有效的文章 Slug")
raise ValueError(f"文章 Slug 无效:{x}")

return f"https://www.jianshu.com/p/{x}"


def notebook_url_to_id(x: str, /) -> int:
if not is_notebook_url(x):
raise ValueError(f"{x} 不是有效的文集链接")
raise ValueError(f"文集链接无效:{x}")

return int(x.replace("https://www.jianshu.com/nb/", "").replace("/", ""))


def notebook_id_to_url(x: int, /) -> str:
if not is_notebook_id(x):
raise ValueError(f"{x} 不是有效的文集 ID")
raise ValueError(f"文集 ID 无效:{x}")

return f"https://www.jianshu.com/nb/{x}"


def collection_url_to_slug(x: str, /) -> str:
if not is_collection_url(x):
raise ValueError(f"{x} 不是有效的专题链接")
raise ValueError(f"专题链接无效:{x}")

return x.replace("https://www.jianshu.com/c/", "").replace("/", "")


def collection_slug_to_url(x: str, /) -> str:
if not is_collection_slug(x):
raise ValueError(f"{x} 不是有效的专题 Slug")
raise ValueError(f"专题 Slug 无效:{x}")

return f"https://www.jianshu.com/c/{x}"


def island_url_to_slug(x: str, /) -> str:
if not is_island_url(x):
raise ValueError(f"{x} 不是有效的小岛链接")
raise ValueError(f"小岛链接无效:{x}")

return x.replace("https://www.jianshu.com/g/", "").replace("/", "")


def island_slug_to_url(x: str, /) -> str:
if not is_island_slug(x):
raise ValueError(f"{x} 不是有效的小岛 Slug")
raise ValueError(f"小岛 Slug 无效:{x}")

return f"https://www.jianshu.com/g/{x}"
4 changes: 2 additions & 2 deletions jkit/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, *, id: int) -> None: # noqa: A002
self._checked = False

if not is_notebook_id(id):
raise ValueError(f"{id} 不是有效的文集 ID")
raise ValueError(f"文集 ID 无效:{id}")
self._id = id

@classmethod
Expand All @@ -125,7 +125,7 @@ async def check(self) -> None:
except HTTPStatusError as e:
if e.response.status_code == 404:
raise ResourceUnavailableError(
f"文集 {self.url} 不存在或已删除"
f"文集 {self.url} 不存在或已被删除"
) from None

raise
Expand Down
11 changes: 8 additions & 3 deletions jkit/ranking/article_earning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from jkit._normalization import normalize_assets_amount
from jkit.config import CONFIG
from jkit.exceptions import APIUnsupportedError
from jkit.identifier_convert import article_slug_to_url
from jkit.msgspec_constraints import (
ArticleSlug,
NonEmptyStr,
Expand Down Expand Up @@ -40,7 +41,11 @@ def is_missing(self) -> bool:

def to_article_obj(self) -> "Article":
if not self.slug:
raise APIUnsupportedError("文章走丢了,可能已被作者删除 / 私密或被锁定")
raise APIUnsupportedError(
f"文章 {article_slug_to_url(self.slug)} 不存在或已被删除 / 私密 / 锁定"
if self.slug
else "文章不存在或已被删除 / 私密 / 锁定"
)

from jkit.article import Article

Expand All @@ -60,9 +65,9 @@ def __init__(self, target_date: Optional[date] = None, /) -> None:
target_date = datetime.now().date() - timedelta(days=1)

if target_date < date(2020, 6, 20):
raise APIUnsupportedError("不支持获取 2020.06.20 前的排行榜数据")
raise APIUnsupportedError("受 API 限制,无法获取 2020.06.20 前的排行榜数据")
if target_date >= datetime.now().date():
raise ValueError("不支持获取未来的排行榜数据")
raise ValueError("无法获取未来的排行榜数据")

self._target_date = target_date

Expand Down
7 changes: 6 additions & 1 deletion jkit/ranking/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from jkit._normalization import normalize_assets_amount
from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from jkit.identifier_convert import user_slug_to_url
from jkit.msgspec_constraints import (
NonNegativeFloat,
PositiveInt,
Expand All @@ -28,7 +29,11 @@ def to_user_obj(self) -> "User":
from jkit.user import User

if not self.slug:
raise ResourceUnavailableError("用户已注销或被封禁")
raise ResourceUnavailableError(
f"用户 {user_slug_to_url(self.slug)} 不存在或已注销 / 被封禁"
if self.slug
else "用户不存在或已注销 / 被封禁"
)

return User.from_slug(self.slug)._as_checked()

Expand Down
4 changes: 2 additions & 2 deletions jkit/ranking/user_earning.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def __init__(
target_date = datetime.now().date() - timedelta(days=1)

if target_date < date(2020, 6, 20):
raise APIUnsupportedError("不支持获取 2020.06.20 前的排行榜数据")
raise APIUnsupportedError("受 API 限制,无法获取 2020.06.20 前的排行榜数据")
if target_date >= datetime.now().date():
raise ValueError("不支持获取未来的排行榜数据")
raise ValueError("无法获取未来的排行榜数据")

self._target_date = target_date
self._type = type
Expand Down

0 comments on commit 83bc1d3

Please sign in to comment.