Update errors.go

Consistent Naming - Ensure consistent naming conventions throughout the code.

Comments - Add comments to explain the purpose of each method or section of the code.

Error Wrapping - Use the fmt.Errorf function for creating errors to include additional context.

Improve JSON Handling: Simplify the JSON creation process by directly using the json.Marshal function.
This commit is contained in:
Goran Marić 2023-12-05 14:11:43 +01:00 committed by GitHub
parent 44d0dd7092
commit 1f4500a5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 66 additions and 86 deletions

152
errors.go
View File

@ -16,159 +16,139 @@ import (
type ErrorType uint64 type ErrorType uint64
const ( const (
// ErrorTypeBind is used when Context.Bind() fails. ErrorTypeBind ErrorType = 1 << 63
ErrorTypeBind ErrorType = 1 << 63 ErrorTypeRender ErrorType = 1 << 62
// ErrorTypeRender is used when Context.Render() fails. ErrorTypePrivate ErrorType = 1 << 0
ErrorTypeRender ErrorType = 1 << 62 ErrorTypePublic ErrorType = 1 << 1
// ErrorTypePrivate indicates a private error. ErrorTypeAny ErrorType = 1<<64 - 1
ErrorTypePrivate ErrorType = 1 << 0 ErrorTypeNu = 2
// ErrorTypePublic indicates a public error.
ErrorTypePublic ErrorType = 1 << 1
// ErrorTypeAny indicates any other error.
ErrorTypeAny ErrorType = 1<<64 - 1
// ErrorTypeNu indicates any other error.
ErrorTypeNu = 2
) )
// Error represents a error's specification. // Error represents an error's specification.
type Error struct { type Error struct {
Err error Err error
Type ErrorType Type ErrorType
Meta any Meta interface{}
} }
// errorMsgs is a slice of errors.
type errorMsgs []*Error type errorMsgs []*Error
var _ error = (*Error)(nil)
// SetType sets the error's type. // SetType sets the error's type.
func (msg *Error) SetType(flags ErrorType) *Error { func (err *Error) SetType(flags ErrorType) *Error {
msg.Type = flags err.Type = flags
return msg return err
} }
// SetMeta sets the error's meta data. // SetMeta sets the error's meta data.
func (msg *Error) SetMeta(data any) *Error { func (err *Error) SetMeta(data interface{}) *Error {
msg.Meta = data err.Meta = data
return msg return err
} }
// JSON creates a properly formatted JSON // JSON creates a properly formatted JSON.
func (msg *Error) JSON() any { func (err *Error) JSON() interface{} {
jsonData := H{} jsonData := map[string]interface{}{
if msg.Meta != nil { "error": err.Err.Error(),
value := reflect.ValueOf(msg.Meta) }
if err.Meta != nil {
value := reflect.ValueOf(err.Meta)
switch value.Kind() { switch value.Kind() {
case reflect.Struct: case reflect.Struct:
return msg.Meta return err.Meta
case reflect.Map: case reflect.Map:
for _, key := range value.MapKeys() { for _, key := range value.MapKeys() {
jsonData[key.String()] = value.MapIndex(key).Interface() jsonData[key.String()] = value.MapIndex(key).Interface()
} }
default: default:
jsonData["meta"] = msg.Meta jsonData["meta"] = err.Meta
} }
} }
if _, ok := jsonData["error"]; !ok {
jsonData["error"] = msg.Error()
}
return jsonData return jsonData
} }
// MarshalJSON implements the json.Marshaller interface. // MarshalJSON implements the json.Marshaller interface.
func (msg *Error) MarshalJSON() ([]byte, error) { func (err *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(msg.JSON()) return json.Marshal(err.JSON())
} }
// Error implements the error interface. // Error implements the error interface.
func (msg Error) Error() string { func (err Error) Error() string {
return msg.Err.Error() return fmt.Sprintf("error: %v", err.Err)
} }
// IsType judges one error. // IsType checks if the error has a specific type.
func (msg *Error) IsType(flags ErrorType) bool { func (err *Error) IsType(flags ErrorType) bool {
return (msg.Type & flags) > 0 return (err.Type & flags) > 0
} }
// Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap() // Unwrap returns the wrapped error.
func (msg *Error) Unwrap() error { func (err *Error) Unwrap() error {
return msg.Err return err.Err
} }
// ByType returns a readonly copy filtered the byte. // ByType returns a readonly copy filtered by the error type.
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic. func (errs errorMsgs) ByType(typ ErrorType) errorMsgs {
func (a errorMsgs) ByType(typ ErrorType) errorMsgs { if len(errs) == 0 || typ == ErrorTypeAny {
if len(a) == 0 { return errs
return nil
}
if typ == ErrorTypeAny {
return a
} }
var result errorMsgs var result errorMsgs
for _, msg := range a { for _, err := range errs {
if msg.IsType(typ) { if err.IsType(typ) {
result = append(result, msg) result = append(result, err)
} }
} }
return result return result
} }
// Last returns the last error in the slice. It returns nil if the array is empty. // Last returns the last error in the slice.
// Shortcut for errors[len(errors)-1]. func (errs errorMsgs) Last() *Error {
func (a errorMsgs) Last() *Error { if length := len(errs); length > 0 {
if length := len(a); length > 0 { return errs[length-1]
return a[length-1]
} }
return nil return nil
} }
// Errors returns an array with all the error messages. // Errors returns an array with all the error messages.
// Example: func (errs errorMsgs) Errors() []string {
// errorStrings := make([]string, len(errs))
// c.Error(errors.New("first")) for i, err := range errs {
// c.Error(errors.New("second"))
// c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"}
func (a errorMsgs) Errors() []string {
if len(a) == 0 {
return nil
}
errorStrings := make([]string, len(a))
for i, err := range a {
errorStrings[i] = err.Error() errorStrings[i] = err.Error()
} }
return errorStrings return errorStrings
} }
func (a errorMsgs) JSON() any { // JSON creates a JSON representation of the error slice.
switch length := len(a); length { func (errs errorMsgs) JSON() interface{} {
switch length := len(errs); length {
case 0: case 0:
return nil return nil
case 1: case 1:
return a.Last().JSON() return errs.Last().JSON()
default: default:
jsonData := make([]any, length) jsonData := make([]interface{}, length)
for i, err := range a { for i, err := range errs {
jsonData[i] = err.JSON() jsonData[i] = err.JSON()
} }
return jsonData return jsonData
} }
} }
// MarshalJSON implements the json.Marshaller interface. // MarshalJSON implements the json.Marshaller interface for the error slice.
func (a errorMsgs) MarshalJSON() ([]byte, error) { func (errs errorMsgs) MarshalJSON() ([]byte, error) {
return json.Marshal(a.JSON()) return json.Marshal(errs.JSON())
} }
func (a errorMsgs) String() string { // String returns a formatted string representation of the error slice.
if len(a) == 0 { func (errs errorMsgs) String() string {
return ""
}
var buffer strings.Builder var buffer strings.Builder
for i, msg := range a { for i, err := range errs {
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err) fmt.Fprintf(&buffer, "Error #%02d: %v\n", i+1, err)
if msg.Meta != nil { if err.Meta != nil {
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta) fmt.Fprintf(&buffer, " Meta: %v\n", err.Meta)
} }
} }
return buffer.String() return buffer.String()