Browse Source

Doc + optim

Brendan Abolivier 7 years ago
parent
commit
13a63682a2
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 11 additions and 5 deletions
  1. 11
    5
      src/grafana/dashboards.go

+ 11
- 5
src/grafana/dashboards.go View File

139
 		Overwrite: true,
139
 		Overwrite: true,
140
 	}
140
 	}
141
 
141
 
142
+	// Generate the request body's JSON
142
 	reqBodyJSON, err := json.Marshal(reqBody)
143
 	reqBodyJSON, err := json.Marshal(reqBody)
143
 	if err != nil {
144
 	if err != nil {
144
 		return
145
 		return
146
 
147
 
147
 	var httpError *httpUnkownError
148
 	var httpError *httpUnkownError
148
 	var isHttpUnknownError bool
149
 	var isHttpUnknownError bool
150
+	// Send the request
149
 	respBodyJSON, err := c.request("POST", "dashboards/db", reqBodyJSON)
151
 	respBodyJSON, err := c.request("POST", "dashboards/db", reqBodyJSON)
150
 	if err != nil {
152
 	if err != nil {
153
+		// Check the error against the httpUnkownError type in order to decide
154
+		// how to process the error
151
 		httpError, isHttpUnknownError = err.(*httpUnkownError)
155
 		httpError, isHttpUnknownError = err.(*httpUnkownError)
152
 		// We process httpUnkownError errors below, after we decoded the body
156
 		// We process httpUnkownError errors below, after we decoded the body
153
 		if !isHttpUnknownError {
157
 		if !isHttpUnknownError {
155
 		}
159
 		}
156
 	}
160
 	}
157
 
161
 
162
+	// Decode the response body
158
 	var respBody dbCreateOrUpdateResponse
163
 	var respBody dbCreateOrUpdateResponse
159
 	if err = json.Unmarshal(respBodyJSON, &respBody); err != nil {
164
 	if err = json.Unmarshal(respBodyJSON, &respBody); err != nil {
160
 		return
165
 		return
161
 	}
166
 	}
162
 
167
 
163
-	slug, err := helpers.GetDashboardSlug(contentJSON)
164
-	if err != nil {
165
-		return
166
-	}
167
-
168
 	if respBody.Status != "success" && isHttpUnknownError {
168
 	if respBody.Status != "success" && isHttpUnknownError {
169
+		// Get the dashboard's slug for logging
170
+		slug, err := helpers.GetDashboardSlug(contentJSON)
171
+		if err != nil {
172
+			return
173
+		}
174
+
169
 		return fmt.Errorf(
175
 		return fmt.Errorf(
170
 			"Failed to update dashboard %s (%d %s): %s",
176
 			"Failed to update dashboard %s (%d %s): %s",
171
 			slug, httpError.StatusCode, respBody.Status, respBody.Message,
177
 			slug, httpError.StatusCode, respBody.Status, respBody.Message,