| 82 | } |
| 83 | |
| 84 | func (h HasuraError) Error() string { |
| 85 | var errorStrings []string |
| 86 | errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s (%s)", h.Code, h.ErrorMessage, h.Path)) |
| 87 | if h.migrationFile != "" { |
| 88 | errorStrings = append(errorStrings, fmt.Sprintf("File: '%s'", h.migrationFile)) |
| 89 | } |
| 90 | if h.migrationQuery != "" { |
| 91 | errorStrings = append(errorStrings, h.migrationQuery) |
| 92 | } |
| 93 | var internalError SQLInternalError |
| 94 | var internalErrors []SQLInternalError |
| 95 | if v, ok := h.Internal.(map[string]interface{}); ok { |
| 96 | err := mapstructure.Decode(v, &internalError) |
| 97 | if err == nil { |
| 98 | // postgres error |
| 99 | if internalError.Error != nil { |
| 100 | errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s: %s", internalError.Error.StatusCode, internalError.Error.ExecStatus, internalError.Error.Message)) |
| 101 | if len(internalError.Error.Description) > 0 { |
| 102 | errorStrings = append(errorStrings, fmt.Sprintf("Description: %s", internalError.Error.Description)) |
| 103 | } |
| 104 | if len(internalError.Error.Hint) > 0 { |
| 105 | errorStrings = append(errorStrings, fmt.Sprintf("Hint: %s", internalError.Error.Hint)) |
| 106 | } |
| 107 | } |
| 108 | if e := internalError.InconsistentMetadataError.String(); e != "" { |
| 109 | errorStrings = append(errorStrings, e) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | if v, ok := h.Internal.([]interface{}); ok { |
| 114 | err := mapstructure.Decode(v, &internalErrors) |
| 115 | if err == nil { |
| 116 | for _, internalError := range internalErrors { |
| 117 | // postgres error |
| 118 | if internalError.Error != nil { |
| 119 | errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s: %s", internalError.Error.StatusCode, internalError.Error.ExecStatus, internalError.Error.Message)) |
| 120 | if len(internalError.Error.Description) > 0 { |
| 121 | errorStrings = append(errorStrings, fmt.Sprintf("Description: %s", internalError.Error.Description)) |
| 122 | } |
| 123 | if len(internalError.Error.Hint) > 0 { |
| 124 | errorStrings = append(errorStrings, fmt.Sprintf("Hint: %s", internalError.Error.Hint)) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if e := internalError.InconsistentMetadataError.String(); e != "" { |
| 129 | errorStrings = append(errorStrings, e) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | if len(errorStrings) == 0 { |
| 135 | return "" |
| 136 | } |
| 137 | return strings.Join(errorStrings, "\r\n") |
| 138 | } |
| 139 | |
| 140 | // NewHasuraError - returns error based on data and isCmd |
| 141 | func NewHasuraError(data []byte, isCmd bool) error { |