github.com/wangyougui/gf/v2@v2.6.5/errors/gcode/gcode.go (about)

     1  // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  // Package gcode provides universal error code definition and common error codes implements.
     8  package gcode
     9  
    10  // Code is universal error code interface definition.
    11  type Code interface {
    12  	// Code returns the integer number of current error code.
    13  	Code() int
    14  
    15  	// Message returns the brief message for current error code.
    16  	Message() string
    17  
    18  	// Detail returns the detailed information of current error code,
    19  	// which is mainly designed as an extension field for error code.
    20  	Detail() interface{}
    21  }
    22  
    23  // ================================================================================================================
    24  // Common error code definition.
    25  // There are reserved internal error code by framework: code < 1000.
    26  // ================================================================================================================
    27  
    28  var (
    29  	CodeNil                       = localCode{-1, "", nil}                             // No error code specified.
    30  	CodeOK                        = localCode{0, "OK", nil}                            // It is OK.
    31  	CodeInternalError             = localCode{50, "Internal Error", nil}               // An error occurred internally.
    32  	CodeValidationFailed          = localCode{51, "Validation Failed", nil}            // Data validation failed.
    33  	CodeDbOperationError          = localCode{52, "Database Operation Error", nil}     // Database operation error.
    34  	CodeInvalidParameter          = localCode{53, "Invalid Parameter", nil}            // The given parameter for current operation is invalid.
    35  	CodeMissingParameter          = localCode{54, "Missing Parameter", nil}            // Parameter for current operation is missing.
    36  	CodeInvalidOperation          = localCode{55, "Invalid Operation", nil}            // The function cannot be used like this.
    37  	CodeInvalidConfiguration      = localCode{56, "Invalid Configuration", nil}        // The configuration is invalid for current operation.
    38  	CodeMissingConfiguration      = localCode{57, "Missing Configuration", nil}        // The configuration is missing for current operation.
    39  	CodeNotImplemented            = localCode{58, "Not Implemented", nil}              // The operation is not implemented yet.
    40  	CodeNotSupported              = localCode{59, "Not Supported", nil}                // The operation is not supported yet.
    41  	CodeOperationFailed           = localCode{60, "Operation Failed", nil}             // I tried, but I cannot give you what you want.
    42  	CodeNotAuthorized             = localCode{61, "Not Authorized", nil}               // Not Authorized.
    43  	CodeSecurityReason            = localCode{62, "Security Reason", nil}              // Security Reason.
    44  	CodeServerBusy                = localCode{63, "Server Is Busy", nil}               // Server is busy, please try again later.
    45  	CodeUnknown                   = localCode{64, "Unknown Error", nil}                // Unknown error.
    46  	CodeNotFound                  = localCode{65, "Not Found", nil}                    // Resource does not exist.
    47  	CodeInvalidRequest            = localCode{66, "Invalid Request", nil}              // Invalid request.
    48  	CodeNecessaryPackageNotImport = localCode{67, "Necessary Package Not Import", nil} // It needs necessary package import.
    49  	CodeInternalPanic             = localCode{68, "Internal Panic", nil}               // An panic occurred internally.
    50  	CodeBusinessValidationFailed  = localCode{300, "Business Validation Failed", nil}  // Business validation failed.
    51  )
    52  
    53  // New creates and returns an error code.
    54  // Note that it returns an interface object of Code.
    55  func New(code int, message string, detail interface{}) Code {
    56  	return localCode{
    57  		code:    code,
    58  		message: message,
    59  		detail:  detail,
    60  	}
    61  }
    62  
    63  // WithCode creates and returns a new error code based on given Code.
    64  // The code and message is from given `code`, but the detail if from given `detail`.
    65  func WithCode(code Code, detail interface{}) Code {
    66  	return localCode{
    67  		code:    code.Code(),
    68  		message: code.Message(),
    69  		detail:  detail,
    70  	}
    71  }