JSON creates a properly formatted JSON
()
| 53 | |
| 54 | // JSON creates a properly formatted JSON |
| 55 | func (msg *Error) JSON() any { |
| 56 | jsonData := H{} |
| 57 | if msg.Meta != nil { |
| 58 | value := reflect.ValueOf(msg.Meta) |
| 59 | switch value.Kind() { |
| 60 | case reflect.Struct: |
| 61 | return msg.Meta |
| 62 | case reflect.Map: |
| 63 | for _, key := range value.MapKeys() { |
| 64 | jsonData[key.String()] = value.MapIndex(key).Interface() |
| 65 | } |
| 66 | default: |
| 67 | jsonData["meta"] = msg.Meta |
| 68 | } |
| 69 | } |
| 70 | if _, ok := jsonData["error"]; !ok { |
| 71 | jsonData["error"] = msg.Error() |
| 72 | } |
| 73 | return jsonData |
| 74 | } |
| 75 | |
| 76 | // MarshalJSON implements the json.Marshaller interface. |
| 77 | func (msg *Error) MarshalJSON() ([]byte, error) { |