gitee.com/hongliu9527/go-tools@v0.0.8/errors/gcode/gcode.go (about)

     1  /*
     2   * @Author: hongliu
     3   * @Date: 2022-12-29 10:51:12
     4   * @LastEditors: hongliu
     5   * @LastEditTime: 2022-12-29 15:53:37
     6   * @FilePath: \go-tools\errors\gcode\gcode.go
     7   * @Description:外部错误处理包接口定义
     8   *
     9   * Copyright (c) 2022 by 洪流, All Rights Reserved.
    10   */
    11  
    12  package gcode
    13  
    14  // Code is universal error code interface definition.
    15  type Code interface {
    16  	// Code returns the integer number of current error code.
    17  	Code() int
    18  
    19  	// Message returns the brief message for current error code.
    20  	Message() string
    21  
    22  	// Detail returns the detailed information of current error code,
    23  	// which is mainly designed as an extension field for error code.
    24  	Detail() interface{}
    25  
    26  	// 返回一个可供序列化的map
    27  	MarshalMap() map[string]interface{}
    28  }
    29  
    30  // ================================================================================================================
    31  // Common error code definition.
    32  // There are reserved internal error code by framework: code < 1000.
    33  // ================================================================================================================
    34  
    35  var (
    36  	CodeNil                      = localCode{-1, "", nil}                            // No error code specified.
    37  	CodeOK                       = localCode{0, "OK", nil}                           // It is OK.
    38  	CodeInternalError            = localCode{50, "Internal Error", nil}              // An error occurred internally.
    39  	CodeValidationFailed         = localCode{51, "Validation Failed", nil}           // Data validation failed.
    40  	CodeDbQueryError             = localCode{52, "Database Query Error", nil}        // Database query error.
    41  	CodeInvalidParameter         = localCode{53, "Invalid Parameter", nil}           // The given parameter for current operation is invalid.
    42  	CodeMissingParameter         = localCode{54, "Missing Parameter", nil}           // Parameter for current operation is missing.
    43  	CodeInvalidOperation         = localCode{55, "Invalid Operation", nil}           // The function cannot be used like this.
    44  	CodeInvalidConfiguration     = localCode{56, "Invalid Configuration", nil}       // The configuration is invalid for current operation.
    45  	CodeMissingConfiguration     = localCode{57, "Missing Configuration", nil}       // The configuration is missing for current operation.
    46  	CodeNotImplemented           = localCode{58, "Not Implemented", nil}             // The operation is not implemented yet.
    47  	CodeNotSupported             = localCode{59, "Not Supported", nil}               // The operation is not supported yet.
    48  	CodeOperationFailed          = localCode{60, "Operation Failed", nil}            // I tried, but I cannot give you what you want.
    49  	CodeNotAuthorized            = localCode{61, "Not Authorized", nil}              // Not Authorized.
    50  	CodeSecurityReason           = localCode{62, "Security Reason", nil}             // Security Reason.
    51  	CodeServerBusy               = localCode{63, "Server Is Busy", nil}              // Server is busy, please try again later.
    52  	CodeUnknown                  = localCode{64, "Unknown Error", nil}               // Unknown error.
    53  	CodeNotFound                 = localCode{65, "Not Found", nil}                   // Resource does not exist.
    54  	CodeInvalidRequest           = localCode{66, "Invalid Request", nil}             // Invalid request.
    55  	CodeDbPersistencyError       = localCode{67, "Database Persistency Error", nil}  // Database persistency error.
    56  	CodeBusinessValidationFailed = localCode{300, "Business Validation Failed", nil} // Business validation failed.
    57  )
    58  
    59  // New creates and returns an error code.
    60  // Note that it returns an interface object of Code.
    61  func New(code int, message string, detail interface{}) Code {
    62  	return localCode{
    63  		code:    code,
    64  		message: message,
    65  		detail:  detail,
    66  	}
    67  }
    68  
    69  // WithCode creates and returns a new error code based on given Code.
    70  // The code and message is from given `code`, but the detail if from given `detail`.
    71  func WithCode(code Code, detail interface{}) Code {
    72  	return localCode{
    73  		code:    code.Code(),
    74  		message: code.Message(),
    75  		detail:  detail,
    76  	}
    77  }