Skip to content

Commit bd5c9a9

Browse files
committed
changes from review
1 parent 7de69ff commit bd5c9a9

3 files changed

Lines changed: 31 additions & 34 deletions

File tree

errors.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package main
2-
3-
// ErrCodeBadContent is reported when the reports content could not be parsed.
4-
const ErrCodeBadContent = "RS_BAD_CONTENT";
5-
// ErrCodeBadHeader is reported when a header was not able to be parsed.
6-
const ErrCodeBadHeader = "RS_BAD_HEADER";
7-
// ErrCodeContentTooLarge is reported when the reported content size is too large.
8-
const ErrCodeContentTooLarge = "RS_CONTENT_TOO_LARGE";
9-
// ErrCodeDisallowedApp is reported when a report was rejected due to the report being sent from an unsupported
10-
const ErrCodeDisallowedApp = "RS_DISALLOWED_APP";
11-
// ErrCodeMethodNotAllowed is reported when you have used the wrong method for an endpoint.
12-
const ErrCodeMethodNotAllowed = "RS_METHOD_NOT_ALLOWED";
13-
// ErrCodeRejected is reported when the submission could be understood but was rejected by RejectionConditions.
14-
const ErrCodeRejected = "RS_REJECTED";
15-
// ErrCodeUnknown is a catch-all error when the appliation does not have a specific error.
16-
const ErrCodeUnknown = "RS_UNKNOWN";
2+
const (
3+
// ErrCodeBadContent is reported when the reports content could not be parsed.
4+
ErrCodeBadContent = "RS_BAD_CONTENT";
5+
// ErrCodeBadHeader is reported when a header was not able to be parsed.
6+
ErrCodeBadHeader = "RS_BAD_HEADER";
7+
// ErrCodeContentTooLarge is reported when the reported content size is too large.
8+
ErrCodeContentTooLarge = "RS_CONTENT_TOO_LARGE";
9+
// ErrCodeDisallowedApp is reported when a report was rejected due to the report being sent from an unsupported
10+
ErrCodeDisallowedApp = "RS_DISALLOWED_APP";
11+
// ErrCodeMethodNotAllowed is reported when you have used the wrong method for an endpoint.
12+
ErrCodeMethodNotAllowed = "RS_METHOD_NOT_ALLOWED";
13+
// ErrCodeRejected is reported when the submission could be understood but was rejected by RejectionConditions.
14+
ErrCodeRejected = "RS_REJECTED";
15+
// ErrCodeUnknown is a catch-all error when the appliation does not have a specific error.
16+
ErrCodeUnknown = "RS_UNKNOWN";
17+
)

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (c RejectionCondition) matchesUserText(p *payload) bool {
154154
return c.UserTextMatch == "" || regexp.MustCompile(c.UserTextMatch).MatchString(p.UserText)
155155
}
156156

157-
// Returns a rejection reason and error code if the payload should be rejected by this condition, otherwise nil.
157+
// Returns a rejection reason and error code if the payload should be rejected by this condition, condition; otherwise returns `nil` for both results.
158158
func (c RejectionCondition) shouldReject(p *payload) (*string, *string) {
159159
if c.matchesApp(p) && c.matchesVersion(p) && c.matchesLabel(p) && c.matchesUserText(p) {
160160
// RejectionCondition matches all of the conditions: we should reject this submission/
@@ -171,7 +171,7 @@ func (c RejectionCondition) shouldReject(p *payload) (*string, *string) {
171171
return nil, nil
172172
}
173173

174-
// Returns a rejection reason and error code if the payload should be rejected by any condition, otherwise nil.
174+
// Returns a rejection reason and error code if the payload should be rejected by any condition, condition; otherwise returns `nil` for both results.
175175
func (c *config) matchesRejectionCondition(p *payload) (*string, *string) {
176176
for _, rc := range c.RejectionConditions {
177177
reject, code := rc.shouldReject(p)
@@ -355,8 +355,8 @@ func loadConfig(configPath string) (*config, error) {
355355
}
356356

357357
for idx, condition := range cfg.RejectionConditions {
358-
if condition.ErrorCode != "" && strings.HasPrefix(condition.ErrorCode, "RS_REJECTED_") == false {
359-
return nil, fmt.Errorf("Rejected condition %d was invalid. ErrorCode must be use the namespace RS_REJECTED_", idx);
358+
if condition.ErrorCode != "" && !strings.HasPrefix(condition.ErrorCode, "RS_REJECTED_") {
359+
return nil, fmt.Errorf("Rejected condition %d was invalid. `errorcode` must be use the namespace RS_REJECTED_", idx);
360360
}
361361
}
362362
return &cfg, nil

submit.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,19 @@ type submitResponse struct {
168168
type submitErrorResponse struct {
169169
Error string `json:"error"`
170170
ErrorCode string `json:"errcode"`
171-
}
172-
173-
type submitPolicyErrorResponse struct {
174-
Error string `json:"error"`
175-
ErrorCode string `json:"errcode"`
176171
PolicyURL string `json:"policy_url,omitempty"`
177172
}
178173

179174

175+
180176
func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
181177
// if we attempt to return a response without reading the request body,
182178
// apache gets upset and returns a 500. Let's try this.
183179
defer req.Body.Close()
184180
defer io.Copy(ioutil.Discard, req.Body)
185181

186182
if req.Method != "POST" && req.Method != "OPTIONS" {
187-
writeError(w, 405, submitErrorResponse{"Method not allowed. Use POST.", ErrCodeMethodNotAllowed})
183+
writeError(w, 405, submitErrorResponse{Error: "Method not allowed. Use POST.", ErrorCode: ErrCodeMethodNotAllowed})
188184
return
189185
}
190186

@@ -210,7 +206,7 @@ func (s *submitServer) handleSubmission(w http.ResponseWriter, req *http.Request
210206
reportDir := filepath.Join("bugs", prefix)
211207
if err := os.MkdirAll(reportDir, os.ModePerm); err != nil {
212208
log.Println("Unable to create report directory", err)
213-
writeError(w, 500, submitErrorResponse{"Internal error", ErrCodeUnknown})
209+
writeError(w, 500, submitErrorResponse{Error: "Internal error", ErrorCode: ErrCodeUnknown})
214210
return
215211
}
216212

@@ -235,7 +231,7 @@ func (s *submitServer) handleSubmission(w http.ResponseWriter, req *http.Request
235231
log.Printf("Unable to remove report dir %s after rejected upload: %v\n",
236232
reportDir, err)
237233
}
238-
writeError(w, 400, submitPolicyErrorResponse{"This server does not accept rageshakes from your application.", ErrCodeDisallowedApp, "https://github.com/matrix-org/rageshake/blob/master/docs/blocked_rageshake.md"})
234+
writeError(w, 400, submitErrorResponse{"This server does not accept rageshakes from your application.", "RS_DISALLOWED_APP", "https://github.com/matrix-org/rageshake/blob/master/docs/blocked_rageshake.md"})
239235
return
240236
}
241237
rejection, code := s.cfg.matchesRejectionCondition(p)
@@ -246,7 +242,7 @@ func (s *submitServer) handleSubmission(w http.ResponseWriter, req *http.Request
246242
reportDir, err)
247243
}
248244
userErrorText := fmt.Sprintf("This server did not accept the rageshake because it matches a rejection condition: %s.", *rejection)
249-
writeError(w, 400, submitPolicyErrorResponse{userErrorText, *code, "https://github.com/matrix-org/rageshake/blob/master/docs/blocked_rageshake.md"})
245+
writeError(w, 400, submitErrorResponse{userErrorText, *code, "https://github.com/matrix-org/rageshake/blob/master/docs/blocked_rageshake.md"})
250246
return
251247
}
252248

@@ -258,7 +254,7 @@ func (s *submitServer) handleSubmission(w http.ResponseWriter, req *http.Request
258254
resp, err := s.saveReport(req.Context(), *p, reportDir, listingURL)
259255
if err != nil {
260256
log.Println("Error handling report submission:", err)
261-
http.Error(w, "Internal error", 500)
257+
writeError(w, 500, submitErrorResponse{Error: "Could not save report", ErrorCode: ErrCodeUnknown})
262258
return
263259
}
264260

@@ -267,7 +263,7 @@ func (s *submitServer) handleSubmission(w http.ResponseWriter, req *http.Request
267263
json.NewEncoder(w).Encode(resp)
268264
}
269265

270-
func writeError(w http.ResponseWriter, status int, response interface{}) {
266+
func writeError(w http.ResponseWriter, status int, response submitErrorResponse) {
271267
w.Header().Set("Content-Type", "application/json")
272268
w.WriteHeader(status)
273269
json.NewEncoder(w).Encode(response)
@@ -279,12 +275,12 @@ func parseRequest(w http.ResponseWriter, req *http.Request, reportDir string) *p
279275
length, err := strconv.Atoi(req.Header.Get("Content-Length"))
280276
if err != nil {
281277
log.Println("Couldn't parse content-length", err)
282-
writeError(w, 400, submitErrorResponse{"Bad Content-Length header", ErrCodeBadHeader})
278+
writeError(w, 400, submitErrorResponse{Error: "Bad Content-Length header", ErrorCode: ErrCodeBadHeader})
283279
return nil
284280
}
285281
if length > maxPayloadSize {
286282
log.Println("Content-length", length, "too large")
287-
writeError(w, 413, submitErrorResponse{"Content too large", ErrCodeContentTooLarge})
283+
writeError(w, 413, submitErrorResponse{Error: "Content too large", ErrorCode: ErrCodeContentTooLarge})
288284
return nil
289285
}
290286

@@ -295,7 +291,7 @@ func parseRequest(w http.ResponseWriter, req *http.Request, reportDir string) *p
295291
p, err1 := parseMultipartRequest(w, req, reportDir)
296292
if err1 != nil {
297293
log.Println("Error parsing multipart data:", err1)
298-
writeError(w, 400, submitErrorResponse{"Bad multipart data", ErrCodeBadContent})
294+
writeError(w, 400, submitErrorResponse{Error: "Bad multipart data", ErrorCode: ErrCodeBadContent})
299295
return nil
300296
}
301297
return p
@@ -305,7 +301,7 @@ func parseRequest(w http.ResponseWriter, req *http.Request, reportDir string) *p
305301
p, err := parseJSONRequest(w, req, reportDir)
306302
if err != nil {
307303
log.Println("Error parsing JSON body", err)
308-
writeError(w, 400, submitErrorResponse{fmt.Sprintf("Could not decode payload: %s", err.Error()), ErrCodeBadContent})
304+
writeError(w, 400, submitErrorResponse{Error: fmt.Sprintf("Could not decode payload: %s", err.Error()), ErrorCode: ErrCodeBadContent})
309305
return nil
310306
}
311307

0 commit comments

Comments
 (0)