坐看云起时|

一枚码农

园龄:7年6个月粉丝:5关注:1

自定义业务状态码

定义

from enum import Enum


class StatusCode(Enum):
    """业务状态码定义"""

    @property
    def code(self):
        """获取业务状态码"""
        return self.value[0]

    @property
    def msg(self):
        """获取状态码信息"""
        return self.value[1]

    @property
    def status_code(self):
        """获取http状态码"""
        return self.value[2] if len(self.value) > 2 else 200

    OK = (0, 'Success')

    # 1 参数验证类
    PARAM_ERROR = (10001, 'Parameter Error')
    NECESSARY_PARAM_ERR = (10002, 'Missing necessary parameter')
    PARAM_NOT_EMPTY = (10002, 'Parameter not Empty')

    # 2 项目业务类
    NO_DATA = (20000, 'No data found')

    # 3 数据库类
    DB_ERROR = (30000, 'Database operation failed')
    DB_INSERT_ERROR = (30001, 'DB create failed')
    DB_UPDATE_ERROR = (30002, 'DB update failed')
    DB_DEL_ERROR = (30003, 'DB delete failed')
    DB_SELECT_ERROR = (30004, 'DB select failed')

    KFK_CONNECT_ERROR = (31000, 'Kafka connect failed')
    KFK_PRODUCER_ERROR = (31000, 'Kafka product failed')
    KFK_CONSUMER_ERROR = (31000, 'Kafka consumer failed')

    # 4 会话类
    LOGIN_EXPIRED = (40000, 'Login expired', 401)
    INVALID_TOKEN = (40001, 'Invalid token', 401)

    # 5 权限类
    NO_ACCESS = (50000, 'No access to interface')

    # 6 网络请求类
    HTTP_ERROR = (60000, 'Http request error', 400)
    HEADER_ERROR = (60001, 'Request header error')

    # 7 文件类
    FILE_NOT_EXIT = (70000, 'File not exit')
    FILE_OPEN_FAIL = (70001, 'Open file fail')
    FILE_WRITE_FILE = (70002, 'Write file fail')
    FILE_READ_FAIL = (70003, 'Read file fail')
    FILE_TYPE_ERROR = (70004, 'File type error')

    # 9 服务器类
    SEVER_ERROR = (90000, 'Server error', 500)


# json 统一返回格式
def json_response(status: StatusCode, data={}, err=''):
    return {'code': status.code, 'msg': f'{status.msg}.{err}', 'data': data}, status.status_code

使用

return json_response(StatusCode.SEVER_ERROR)

本文作者:一枚码农

本文链接:https://www.cnblogs.com/yimeimanong/p/16775602.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   一枚码农  阅读(169)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
 
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sold Out Hawk
  2. 2 光辉岁月 Beyond
Sold Out - Hawk
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Jon Steingard

作曲 : Jon Steingard

I ain't like no one you met before

I'm running for the front

When they're all running for the door

And I won't sit down won't back out

You can't ever shut me up

Cause I'm on a mission

And I won't quit now

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Because the battle's already been won

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

This ain't just some temporary phase

You can't face this kind of grace

And leave the way you came

This is permanent with intent

And there won't be no stopping it now

I'm on a mission and it's heaven sent

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Cause my soul is like a stadium

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to shout

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

No trials coming against me

Could put a dent in my passion

They're just an opportunity

To put my faith into action

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

I ain't got nothing left to be afraid of

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out