<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] > [參考](https://juejin.cn/post/7025223216473833486) ## 概述 server 層的代碼 ``` type LoginService struct{} func (s *LoginService) Login(req *model.LoginReq) (*model.LoginRsp, result.Error) { if req.Username != "admin" || req.Password != "admin" { return nil, result.NewError(result.ErrCodeInvalidParameterUsernameOrPassword) } return &model.LoginRsp{ Token: uuid.New().String(), }, nil } ``` req和rsp都是簡單的結構體 ``` type LoginReq struct { Username string `json:"username"` Password string `json:"password"` } type LoginRsp struct { Token string `json:"token"` } ``` <details> <summary>error.go</summary> ``` import ( "errors" ) type Unwrap interface { Unwrap() error } type Error interface { error Unwrap ErrCode } // ErrorImpl 錯誤 type ErrorImpl struct { error ErrCode } // Unwrap 實現 Unwrap 接口 func (e ErrorImpl) Unwrap() error { return e.error } // WrapError 包裝 error func WrapError(errCode ErrCode, err error) Error { return ErrorImpl{ error: err, ErrCode: errCode, } } // NewError 新建一個 Error func NewError(errCode ErrCode) Error { return ErrorImpl{ error: errors.New(errCode.Msg()), ErrCode: errCode, } } ``` </details> <br/> <details> <summary>error_code.go</summary> ``` // ErrCode 錯誤碼 type ErrCode interface { Status() int // HTTP狀態碼 Code() string // 錯誤碼 Msg() string // 錯誤消息 Advice() string // 建議處理方式 } // errCodeImpl 錯誤碼實現 type errCodeImpl struct { status int code string msg string advice string } func (e errCodeImpl) Status() int { return e.status } func (e errCodeImpl) Code() string { return e.code } func (e errCodeImpl) Msg() string { return e.msg } func (e errCodeImpl) Advice() string { return e.advice } // newErrCode 新建一個錯誤碼 func newErrCode(status int, code, msg, advice string) ErrCode { return errCodeImpl{ code: code, msg: msg, status: status, advice: advice, } } ``` </details> <br/> <details> <summary>gin_rsp.go</summary> ``` import ( "github.com/gin-gonic/gin" ) // Rsp 響應 type Rsp struct { Code string `json:"code,omitempty"` // 錯誤碼 Msg string `json:"msg,omitempty"` // 消息 Data interface{} `json:"data,omitempty"` // 數據 } // Success 請求成功 func Success(c *gin.Context, data interface{}) { rsp(c, ErrCodeOK, data) } // Failure 請求失敗 func Failure(c *gin.Context, errCode ErrCode) { rsp(c, errCode, nil) } // rsp 響應 func rsp(c *gin.Context, errCode ErrCode, data interface{}) { c.JSON(errCode.Status(), &Rsp{ Code: errCode.Code(), Msg: errCode.Advice(), Data: data, }) } ``` </details> <br/> <details> <summary>err_codes.go</summary> ``` var ( ErrCodeOK = newErrCode(http.StatusOK, "OK", "", "") ErrCodeInvalidParameter = newErrCode(http.StatusBadRequest, "InvalidParameter", "The required parameter is not valid.", "非法參數") ErrCodeInvalidParameterUsernameOrPassword = newErrCode(http.StatusBadRequest, "InvalidParameter.UsernameOrPassword", "The username or password is not valid.", "賬號或密碼錯誤") ErrCodeInternalError = newErrCode(http.StatusInternalServerError, "InternalError", "The request processing has failed due to some unknown error.", "給您帶來的不便,深感抱歉,請稍后再試") ) ``` </details> <br/>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看