github.com/polarismesh/polaris@v1.17.8/store/status.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package store
    19  
    20  import (
    21  	"strings"
    22  )
    23  
    24  // StatusCode 存储层的状态码
    25  type StatusCode uint32
    26  
    27  // 状态码定义
    28  const (
    29  	Ok                            StatusCode = iota
    30  	EmptyParamsErr                           // 参数不合法
    31  	OutOfRangeErr                            // 数据不合法,比如越级了,超过了字段大小
    32  	DataConflictErr                          // 数据冲突,在并发更新metadata的时候可能会出现
    33  	NotFoundNamespace                        // 找不到namespace,service插入依赖namespace是否存在
    34  	NotFoundService                          // 找不到service,在instance等资源插入的时候依赖service是否存在
    35  	NotFoundMasterConfig                     // 在标记规则前,需要保证规则的master版本存在
    36  	NotFoundTagConfigOrService               // 在发布规则前,需要保证规则已标记且服务存在
    37  	ExistReleasedConfig                      // 在删除规则时,发现存在已经发布的版本
    38  	AffectedRowsNotMatch                     // 操作的行数与预期不符合
    39  	DuplicateEntryErr                        // 主键重复,一般是资源已存在了,提醒用户资源存在
    40  	ForeignKeyErr                            // 外键错误,一般是操作不当导致的
    41  	DeadlockErr                              // 数据库死锁
    42  	NotFoundMeshOrService                    // 网格订阅服务的时候,网格或者服务不存在
    43  	NotFoundMeshService                      // 更新订阅服务的时候,订阅服务不存在
    44  	NotFoundCircuitBreaker                   // Failed to find target CircuitBreaker
    45  	NotFoundReleaseCircuitBreaker            // Failed to find fuse breaker information associated with service
    46  	Unknown
    47  	NotFoundUser       // 用户不存在
    48  	NotFoundUserGroup  // 用户组不存在
    49  	InvalidUserIDSlice // 非法的用户ID列表
    50  )
    51  
    52  // Error 普通error转StatusError
    53  func Error(err error) error {
    54  	if err == nil {
    55  		return nil
    56  	}
    57  
    58  	// 已经是StatusError了,不再转换
    59  	if _, ok := err.(*StatusError); ok {
    60  		return err
    61  	}
    62  
    63  	s := &StatusError{message: err.Error()}
    64  	if strings.Contains(s.message, "Data too long") {
    65  		s.code = OutOfRangeErr
    66  	} else if strings.Contains(s.message, "Duplicate entry") {
    67  		s.code = DuplicateEntryErr
    68  	} else if strings.Contains(s.message, "a foreign key constraint fails") {
    69  		s.code = ForeignKeyErr
    70  	} else if strings.Contains(s.message, "Deadlock") {
    71  		s.code = DeadlockErr
    72  	} else {
    73  		s.code = Unknown
    74  	}
    75  
    76  	return s
    77  }
    78  
    79  // NewStatusError 根据code和message创建StatusError
    80  func NewStatusError(code StatusCode, message string) error {
    81  	return &StatusError{
    82  		code:    code,
    83  		message: message,
    84  	}
    85  }
    86  
    87  // Code 根据error接口,获取状态码
    88  func Code(err error) StatusCode {
    89  	if err == nil {
    90  		return Ok
    91  	}
    92  
    93  	se, ok := err.(*StatusError)
    94  	if ok {
    95  		return se.code
    96  	}
    97  
    98  	return Unknown
    99  }
   100  
   101  // StatusError 包括了状态码的error接口
   102  type StatusError struct {
   103  	code    StatusCode
   104  	message string
   105  }
   106  
   107  // Error 实现error接口
   108  func (s *StatusError) Error() string {
   109  	if s == nil {
   110  		return ""
   111  	}
   112  
   113  	return s.message
   114  }