github.com/erda-project/erda-infra@v1.0.9/providers/legacy/httpendpoints/errorresp/error_codes.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package errorresp
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  
    21  	"github.com/erda-project/erda-infra/providers/legacy/httpendpoints/i18n"
    22  )
    23  
    24  // Error Codes i18n templates
    25  var (
    26  	templateMissingParameter      = i18n.NewTemplate("MissingParameter", "缺少参数 %s")
    27  	templateInvalidParameter      = i18n.NewTemplate("InvalidParameter", "参数错误 %s")
    28  	templateInvalidState          = i18n.NewTemplate("InvalidState", "状态异常 %s")
    29  	templateNotLogin              = i18n.NewTemplate("NotLogin", "未登录")
    30  	templateAccessDenied          = i18n.NewTemplate("AccessDenied", "无权限")
    31  	templateNotFound              = i18n.NewTemplate("NotFound", "资源不存在")
    32  	templateAlreadyExists         = i18n.NewTemplate("AlreadyExists", "资源已存在")
    33  	templateInternalError         = i18n.NewTemplate("InternalError", "异常 %s")
    34  	templateErrorVerificationCode = i18n.NewTemplate("ErrorVerificationCode", "验证码错误 %s")
    35  )
    36  
    37  // MissingParameter .
    38  func (e *APIError) MissingParameter(err string) *APIError {
    39  	return e.dup().appendCode(http.StatusBadRequest, "MissingParameter").
    40  		appendLocaleTemplate(templateMissingParameter, err)
    41  }
    42  
    43  // InvalidParameter .
    44  func (e *APIError) InvalidParameter(err interface{}) *APIError {
    45  	return e.dup().appendCode(http.StatusBadRequest, "InvalidParameter").
    46  		appendLocaleTemplate(templateInvalidParameter, toString(err))
    47  }
    48  
    49  // InvalidState .
    50  func (e *APIError) InvalidState(err string) *APIError {
    51  	return e.dup().appendCode(http.StatusBadRequest, "InvalidState").
    52  		appendLocaleTemplate(templateInvalidState, err)
    53  }
    54  
    55  // NotLogin .
    56  func (e *APIError) NotLogin() *APIError {
    57  	return e.dup().appendCode(http.StatusUnauthorized, "NotLogin").
    58  		appendLocaleTemplate(templateNotLogin)
    59  }
    60  
    61  // AccessDenied .
    62  func (e *APIError) AccessDenied() *APIError {
    63  	return e.dup().appendCode(http.StatusForbidden, "AccessDenied").
    64  		appendLocaleTemplate(templateAccessDenied)
    65  }
    66  
    67  // NotFound .
    68  func (e *APIError) NotFound() *APIError {
    69  	return e.dup().appendCode(http.StatusNotFound, "NotFound").
    70  		appendLocaleTemplate(templateNotFound)
    71  }
    72  
    73  // IsNotFound .
    74  func IsNotFound(e error) bool {
    75  	return getCode(e) == "NotFound"
    76  }
    77  
    78  // AlreadyExists .
    79  func (e *APIError) AlreadyExists() *APIError {
    80  	return e.dup().appendCode(http.StatusConflict, "AlreadyExists").
    81  		appendLocaleTemplate(templateAlreadyExists)
    82  }
    83  
    84  // InternalError .
    85  func (e *APIError) InternalError(err error) *APIError {
    86  	return e.dup().appendCode(http.StatusInternalServerError, "InternalError").
    87  		appendLocaleTemplate(templateInternalError, err.Error())
    88  }
    89  
    90  // ErrorVerificationCode .
    91  func (e *APIError) ErrorVerificationCode(err error) *APIError {
    92  	return e.dup().appendCode(http.StatusInternalServerError, "ErrorVerificationCode").
    93  		appendLocaleTemplate(templateErrorVerificationCode, err.Error())
    94  }
    95  
    96  func toString(err interface{}) string {
    97  	switch t := err.(type) {
    98  	case string:
    99  		return err.(string)
   100  	case error:
   101  		return err.(error).Error()
   102  	default:
   103  		_ = t
   104  		return fmt.Sprintf("%v", err)
   105  	}
   106  }
   107  
   108  func getCode(e error) string {
   109  	switch t := e.(type) {
   110  	case *APIError:
   111  		return t.code
   112  	default:
   113  		return ""
   114  	}
   115  }