add error code for components

This commit is contained in:
John Guo
2021-07-20 23:02:02 +08:00
parent f72d991c36
commit 0ddacdd7e2
88 changed files with 434 additions and 263 deletions

View File

@ -49,7 +49,7 @@ func New(text string) error {
return &Error{
stack: callers(),
text: text,
code: -1,
code: CodeNil,
}
}
@ -58,7 +58,7 @@ func Newf(format string, args ...interface{}) error {
return &Error{
stack: callers(),
text: fmt.Sprintf(format, args...),
code: -1,
code: CodeNil,
}
}
@ -68,7 +68,7 @@ func NewSkip(skip int, text string) error {
return &Error{
stack: callers(skip),
text: text,
code: -1,
code: CodeNil,
}
}
@ -78,7 +78,7 @@ func NewSkipf(skip int, format string, args ...interface{}) error {
return &Error{
stack: callers(skip),
text: fmt.Sprintf(format, args...),
code: -1,
code: CodeNil,
}
}

View File

@ -0,0 +1,25 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gerror
// Reserved internal error code of framework: code < 1000.
const (
CodeNil = -1 // No error code specified.
CodeOk = 0 // It is OK without error.
CodeInternalError = 50 // An error occurred internally.
CodeValidationFailed = 51 // Data validation failed.
CodeDbOperationError = 52 // Database operation error.
CodeInvalidParameter = 53 // The given parameter for current operation is invalid.
CodeMissingParameter = 54 // Parameter for current operation is missing.
CodeInvalidOperation = 55 // The function cannot be used like this.
CodeInvalidConfiguration = 56 // The configuration is invalid for current operation.
CodeMissingConfiguration = 57 // The configuration is missing for current operation.
CodeNotImplemented = 58 // The operation is not implemented yet.
CodeNotSupported = 59 // The operation is not supported yet.
CodeOperationFailed = 60 // I tried, but I cannot give you what you want.
)

View File

@ -179,6 +179,9 @@ func (err *Error) MarshalJSON() ([]byte, error) {
// formatSubStack formats the stack for error.
func formatSubStack(st stack, buffer *bytes.Buffer) {
if st == nil {
return
}
index := 1
space := " "
for _, p := range st {

View File

@ -0,0 +1,28 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gerror
// Option is option for creating error.
type Option struct {
Error error // Wrapped error.
Stack bool // Record stack information into error.
Text string // Error text, which is created by New* functions.
Code int // Error code if necessary.
}
// NewOption creates and returns an error with Option.
func NewOption(option Option) error {
err := &Error{
error: option.Error,
text: option.Text,
code: option.Code,
}
if option.Stack {
err.stack = callers()
}
return err
}