New JSON error facilities
This commit is contained in:
		
							
								
								
									
										35
									
								
								errors.go
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								errors.go
									
									
									
									
									
								
							@ -7,6 +7,7 @@ package gin
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@ -36,6 +37,25 @@ func (msg *errorMsg) Meta(data interface{}) *errorMsg {
 | 
				
			|||||||
	return msg
 | 
						return msg
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (msg *errorMsg) JSON() interface{} {
 | 
				
			||||||
 | 
						json := H{}
 | 
				
			||||||
 | 
						if msg.Metadata != nil {
 | 
				
			||||||
 | 
							value := reflect.ValueOf(msg.Metadata)
 | 
				
			||||||
 | 
							switch value.Kind() {
 | 
				
			||||||
 | 
							case reflect.Struct:
 | 
				
			||||||
 | 
								return msg.Metadata
 | 
				
			||||||
 | 
							case reflect.Map:
 | 
				
			||||||
 | 
								for _, key := range value.MapKeys() {
 | 
				
			||||||
 | 
									json[key.String()] = value.MapIndex(key).Interface()
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if _, ok := json["error"]; !ok {
 | 
				
			||||||
 | 
							json["error"] = msg.Error()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return json
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (msg *errorMsg) Error() string {
 | 
					func (msg *errorMsg) Error() string {
 | 
				
			||||||
	return msg.Err.Error()
 | 
						return msg.Err.Error()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -74,6 +94,21 @@ func (a errorMsgs) Errors() []string {
 | 
				
			|||||||
	return errorStrings
 | 
						return errorStrings
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a errorMsgs) JSON() interface{} {
 | 
				
			||||||
 | 
						switch len(a) {
 | 
				
			||||||
 | 
						case 0:
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						case 1:
 | 
				
			||||||
 | 
							return a.Last().JSON()
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							json := make([]interface{}, len(a))
 | 
				
			||||||
 | 
							for i, err := range a {
 | 
				
			||||||
 | 
								json[i] = err.JSON()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return json
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a errorMsgs) String() string {
 | 
					func (a errorMsgs) String() string {
 | 
				
			||||||
	if len(a) == 0 {
 | 
						if len(a) == 0 {
 | 
				
			||||||
		return ""
 | 
							return ""
 | 
				
			||||||
 | 
				
			|||||||
@ -30,8 +30,9 @@ func ErrorLoggerT(typ int) HandlerFunc {
 | 
				
			|||||||
		c.Next()
 | 
							c.Next()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if !c.Writer.Written() {
 | 
							if !c.Writer.Written() {
 | 
				
			||||||
			if errs := c.Errors.ByType(typ); len(errs) > 0 {
 | 
								json := c.Errors.ByType(typ).JSON()
 | 
				
			||||||
				c.JSON(-1, errs.Errors())
 | 
								if json != nil {
 | 
				
			||||||
 | 
									c.JSON(-1, json)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user