跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Coding] rpc 服务中,“业务错误”的返回应该如何设计?

发表于

是返回类似下面的业务错误码结构,

{
    "rc": 10001,
    "msg": "部分商品没有库存",
    "data": {
        "out_of_stock_ids": [
            1,
            2,
            3
        ]
    }
}

还是抛出下面这样的自定义异常?

class BaseBusinessError(Exception):
    rc = 500
    msg = 'Unknown error'
    data = None

    def __repr__(self):
        return f'<Error {self.rc}: {self.msg}. data: {self.data}>'

    def __str__(self):
        return self.__repr__()


class ShopOutOfStockError(BaseBusinessError):
    rc = 10001
    msg = "部分商品没有库存"
    data = None

    def __init__(self, product_ids: list):
        self.data = product_ids

如果采用异常方案,调用方没有自定义异常类的定义怎么办?如何反序列化?是不是要共享异常的定义代码给所有调用方?要是调用方是其它语言呢?

这篇博客 下的这句:

查询方法不建议抛出 checked 异常,否则调用方在查询时将过多的 try...catch ,并且不能进行有效处理。

中的 checked 异常是什么意思?是否可以理解为查询方法不抛异常。比如,查询某个数据没有权限,直接返回空,而不是抛一个没有权限的异常。

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.