Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: ct/go/ctfe/task_common/task_common.go

Issue 1820863002: Update callers of httputils.ReportError to specify message (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Do not duplicate fmt.Errorf and message Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 Handlers, types, and functions common to all types of tasks. 2 Handlers, types, and functions common to all types of tasks.
3 */ 3 */
4 4
5 package task_common 5 package task_common
6 6
7 import ( 7 import (
8 "database/sql" 8 "database/sql"
9 "encoding/json" 9 "encoding/json"
10 "fmt" 10 "fmt"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 func (vars *AddTaskCommonVars) GetAddTaskCommonVars() *AddTaskCommonVars { 91 func (vars *AddTaskCommonVars) GetAddTaskCommonVars() *AddTaskCommonVars {
92 return vars 92 return vars
93 } 93 }
94 94
95 func (vars *AddTaskCommonVars) IsAdminTask() bool { 95 func (vars *AddTaskCommonVars) IsAdminTask() bool {
96 return false 96 return false
97 } 97 }
98 98
99 func AddTaskHandler(w http.ResponseWriter, r *http.Request, task AddTaskVars) { 99 func AddTaskHandler(w http.ResponseWriter, r *http.Request, task AddTaskVars) {
100 if !ctfeutil.UserHasEditRights(r) { 100 if !ctfeutil.UserHasEditRights(r) {
101 » » httputils.ReportError(w, r, fmt.Errorf("Must have google or chro mium account to add tasks"), "") 101 » » httputils.ReportError(w, r, nil, "Please login with google or ch romium account to add tasks")
102 return 102 return
103 } 103 }
104 if task.IsAdminTask() && !ctfeutil.UserHasAdminRights(r) { 104 if task.IsAdminTask() && !ctfeutil.UserHasAdminRights(r) {
105 » » httputils.ReportError(w, r, fmt.Errorf("Must be admin to add adm in tasks; contact rmistry@"), "") 105 » » httputils.ReportError(w, r, nil, "Must be admin to add admin tas ks; contact rmistry@")
106 return 106 return
107 } 107 }
108 w.Header().Set("Content-Type", "application/json") 108 w.Header().Set("Content-Type", "application/json")
109 if err := json.NewDecoder(r.Body).Decode(&task); err != nil { 109 if err := json.NewDecoder(r.Body).Decode(&task); err != nil {
110 httputils.ReportError(w, r, err, fmt.Sprintf("Failed to add %T t ask", task)) 110 httputils.ReportError(w, r, err, fmt.Sprintf("Failed to add %T t ask", task))
111 return 111 return
112 } 112 }
113 defer skutil.Close(r.Body) 113 defer skutil.Close(r.Body)
114 114
115 task.GetAddTaskCommonVars().Username = login.LoggedInAs(r) 115 task.GetAddTaskCommonVars().Username = login.LoggedInAs(r)
116 task.GetAddTaskCommonVars().TsAdded = ctutil.GetCurrentTs() 116 task.GetAddTaskCommonVars().TsAdded = ctutil.GetCurrentTs()
117 if len(task.GetAddTaskCommonVars().Username) > 255 { 117 if len(task.GetAddTaskCommonVars().Username) > 255 {
118 » » httputils.ReportError(w, r, fmt.Errorf("Username is too long, li mit 255 bytes"), "") 118 » » httputils.ReportError(w, r, nil, "Username is too long, limit 25 5 bytes")
119 return 119 return
120 } 120 }
121 121
122 if _, err := AddTask(task); err != nil { 122 if _, err := AddTask(task); err != nil {
123 httputils.ReportError(w, r, err, fmt.Sprintf("Failed to insert % T task", task)) 123 httputils.ReportError(w, r, err, fmt.Sprintf("Failed to insert % T task", task))
124 return 124 return
125 } 125 }
126 } 126 }
127 127
128 // Returns the ID of the inserted task if the operation was successful. 128 // Returns the ID of the inserted task if the operation was successful.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 if !params.CountQuery { 201 if !params.CountQuery {
202 query += " ORDER BY id DESC LIMIT ?,?" 202 query += " ORDER BY id DESC LIMIT ?,?"
203 args = append(args, params.Offset, params.Size) 203 args = append(args, params.Offset, params.Size)
204 } 204 }
205 return query, args 205 return query, args
206 } 206 }
207 207
208 func GetTaskStatusHandler(prototype Task, w http.ResponseWriter, r *http.Request ) { 208 func GetTaskStatusHandler(prototype Task, w http.ResponseWriter, r *http.Request ) {
209 taskID := r.FormValue("task_id") 209 taskID := r.FormValue("task_id")
210 if taskID == "" { 210 if taskID == "" {
211 » » httputils.ReportError(w, r, fmt.Errorf("Missing required paramet er task_id"), "") 211 » » httputils.ReportError(w, r, nil, "Missing required parameter tas k_id")
212 return 212 return
213 } 213 }
214 214
215 rowQuery := fmt.Sprintf("SELECT * FROM %s WHERE id = ?", prototype.Table Name()) 215 rowQuery := fmt.Sprintf("SELECT * FROM %s WHERE id = ?", prototype.Table Name())
216 data, err := prototype.Select(rowQuery, taskID) 216 data, err := prototype.Select(rowQuery, taskID)
217 if err != nil { 217 if err != nil {
218 » » httputils.ReportError(w, r, fmt.Errorf("Could not find %s task w ith ID %s", prototype.GetTaskName(), taskID), "") 218 » » httputils.ReportError(w, r, nil, fmt.Sprintf("Could not find %s task with ID %s", prototype.GetTaskName(), taskID))
dogben 2016/03/21 18:54:38 taskID is not sanitized. Same below.
rmistry 2016/03/21 18:56:42 What do you mean by sanitized?
219 return 219 return
220 } 220 }
221 tasks := AsTaskSlice(data) 221 tasks := AsTaskSlice(data)
222 if len(tasks) == 0 { 222 if len(tasks) == 0 {
223 » » httputils.ReportError(w, r, fmt.Errorf("Could not find %s task w ith ID %s", prototype.GetTaskName(), taskID), "") 223 » » httputils.ReportError(w, r, nil, fmt.Sprintf("Could not find %s task with ID %s", prototype.GetTaskName(), taskID))
224 return 224 return
225 } 225 }
226 226
227 status := "Pending" 227 status := "Pending"
228 resultsLink := tasks[0].GetResultsLink() 228 resultsLink := tasks[0].GetResultsLink()
229 if tasks[0].GetCommonCols().TsCompleted.Valid { 229 if tasks[0].GetCommonCols().TsCompleted.Valid {
230 status = "Completed" 230 status = "Completed"
231 if tasks[0].GetCommonCols().Failure.Valid && tasks[0].GetCommonC ols().Failure.Bool { 231 if tasks[0].GetCommonCols().Failure.Valid && tasks[0].GetCommonC ols().Failure.Bool {
232 status += " with failures" 232 status += " with failures"
233 } 233 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 func GetTasksHandler(prototype Task, w http.ResponseWriter, r *http.Request) { 265 func GetTasksHandler(prototype Task, w http.ResponseWriter, r *http.Request) {
266 w.Header().Set("Content-Type", "application/json") 266 w.Header().Set("Content-Type", "application/json")
267 267
268 params := QueryParams{} 268 params := QueryParams{}
269 params.Username = r.FormValue("username") 269 params.Username = r.FormValue("username")
270 params.SuccessfulOnly = parseBoolFormValue(r.FormValue("successful")) 270 params.SuccessfulOnly = parseBoolFormValue(r.FormValue("successful"))
271 params.PendingOnly = parseBoolFormValue(r.FormValue("not_completed")) 271 params.PendingOnly = parseBoolFormValue(r.FormValue("not_completed"))
272 params.FutureRunsOnly = parseBoolFormValue(r.FormValue("include_future_r uns")) 272 params.FutureRunsOnly = parseBoolFormValue(r.FormValue("include_future_r uns"))
273 params.ExcludeDummyPageSets = parseBoolFormValue(r.FormValue("exclude_du mmy_page_sets")) 273 params.ExcludeDummyPageSets = parseBoolFormValue(r.FormValue("exclude_du mmy_page_sets"))
274 if params.SuccessfulOnly && params.PendingOnly { 274 if params.SuccessfulOnly && params.PendingOnly {
275 » » httputils.ReportError(w, r, fmt.Errorf("Inconsistent params: suc cessful %v not_completed %v", r.FormValue("successful"), r.FormValue("not_comple ted")), "") 275 » » httputils.ReportError(w, r, fmt.Errorf("Inconsistent params: suc cessful %v not_completed %v", r.FormValue("successful"), r.FormValue("not_comple ted")), "Inconsistent params")
276 return 276 return
277 } 277 }
278 if params.ExcludeDummyPageSets && !HasPageSetsColumn(prototype) { 278 if params.ExcludeDummyPageSets && !HasPageSetsColumn(prototype) {
279 » » httputils.ReportError(w, r, fmt.Errorf("Task %s does not use pag e sets and thus cannot exclude dummy page sets.", prototype.GetTaskName()), "") 279 » » httputils.ReportError(w, r, nil, fmt.Sprintf("Task %s does not u se page sets and thus cannot exclude dummy page sets.", prototype.GetTaskName()) )
280 return 280 return
281 } 281 }
282 offset, size, err := httputils.PaginationParams(r.URL.Query(), 0, DEFAUL T_PAGE_SIZE, MAX_PAGE_SIZE) 282 offset, size, err := httputils.PaginationParams(r.URL.Query(), 0, DEFAUL T_PAGE_SIZE, MAX_PAGE_SIZE)
283 if err == nil { 283 if err == nil {
284 params.Offset, params.Size = offset, size 284 params.Offset, params.Size = offset, size
285 } else { 285 } else {
286 httputils.ReportError(w, r, err, "Failed to get pagination param s") 286 httputils.ReportError(w, r, err, "Failed to get pagination param s")
287 return 287 return
288 } 288 }
289 params.CountQuery = false 289 params.CountQuery = false
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 // error describing the problem. 463 // error describing the problem.
464 func canRedoTask(task Task, r *http.Request) (bool, error) { 464 func canRedoTask(task Task, r *http.Request) (bool, error) {
465 if !task.GetCommonCols().TsCompleted.Valid { 465 if !task.GetCommonCols().TsCompleted.Valid {
466 return false, fmt.Errorf("Cannot redo pending tasks.") 466 return false, fmt.Errorf("Cannot redo pending tasks.")
467 } 467 }
468 return true, nil 468 return true, nil
469 } 469 }
470 470
471 func DeleteTaskHandler(prototype Task, w http.ResponseWriter, r *http.Request) { 471 func DeleteTaskHandler(prototype Task, w http.ResponseWriter, r *http.Request) {
472 if !ctfeutil.UserHasEditRights(r) { 472 if !ctfeutil.UserHasEditRights(r) {
473 » » httputils.ReportError(w, r, fmt.Errorf("Must have google or chro mium account to delete tasks"), "") 473 » » httputils.ReportError(w, r, nil, "Please login with google or ch romium account to delete tasks")
474 return 474 return
475 } 475 }
476 w.Header().Set("Content-Type", "application/json") 476 w.Header().Set("Content-Type", "application/json")
477 vars := struct{ Id int64 }{} 477 vars := struct{ Id int64 }{}
478 if err := json.NewDecoder(r.Body).Decode(&vars); err != nil { 478 if err := json.NewDecoder(r.Body).Decode(&vars); err != nil {
479 httputils.ReportError(w, r, err, "Failed to parse delete request ") 479 httputils.ReportError(w, r, err, "Failed to parse delete request ")
480 return 480 return
481 } 481 }
482 defer skutil.Close(r.Body) 482 defer skutil.Close(r.Body)
483 requireUsernameMatch := !ctfeutil.UserHasAdminRights(r) 483 requireUsernameMatch := !ctfeutil.UserHasAdminRights(r)
(...skipping 21 matching lines...) Expand all
505 if err != nil { 505 if err != nil {
506 httputils.ReportError(w, r, err, "Unable to validate request.") 506 httputils.ReportError(w, r, err, "Unable to validate request.")
507 return 507 return
508 } 508 }
509 tasks := AsTaskSlice(data) 509 tasks := AsTaskSlice(data)
510 if len(tasks) != 1 { 510 if len(tasks) != 1 {
511 // Row already deleted; return success. 511 // Row already deleted; return success.
512 return 512 return
513 } 513 }
514 if ok, err := canDeleteTask(tasks[0], r); !ok { 514 if ok, err := canDeleteTask(tasks[0], r); !ok {
515 » » httputils.ReportError(w, r, err, "") 515 » » httputils.ReportError(w, r, err, "Do not have permission to dele te task")
516 } else { 516 } else {
517 » » httputils.ReportError(w, r, fmt.Errorf("Failed to delete; reason unknown"), "") 517 » » httputils.ReportError(w, r, nil, "Failed to delete; reason unkno wn")
518 return 518 return
519 } 519 }
520 } 520 }
521 521
522 func RedoTaskHandler(prototype Task, w http.ResponseWriter, r *http.Request) { 522 func RedoTaskHandler(prototype Task, w http.ResponseWriter, r *http.Request) {
523 if !ctfeutil.UserHasEditRights(r) { 523 if !ctfeutil.UserHasEditRights(r) {
524 » » httputils.ReportError(w, r, fmt.Errorf("Must have google or chro mium account to redo tasks"), "") 524 » » httputils.ReportError(w, r, nil, "Please login with google or ch romium account to redo tasks")
525 return 525 return
526 } 526 }
527 w.Header().Set("Content-Type", "application/json") 527 w.Header().Set("Content-Type", "application/json")
528 vars := struct{ Id int64 }{} 528 vars := struct{ Id int64 }{}
529 if err := json.NewDecoder(r.Body).Decode(&vars); err != nil { 529 if err := json.NewDecoder(r.Body).Decode(&vars); err != nil {
530 httputils.ReportError(w, r, err, "Failed to parse redo request") 530 httputils.ReportError(w, r, err, "Failed to parse redo request")
531 return 531 return
532 } 532 }
533 defer skutil.Close(r.Body) 533 defer skutil.Close(r.Body)
534 534
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 568
569 if err := json.NewEncoder(w).Encode(pageSets); err != nil { 569 if err := json.NewEncoder(w).Encode(pageSets); err != nil {
570 httputils.ReportError(w, r, err, fmt.Sprintf("Failed to encode J SON: %v", err)) 570 httputils.ReportError(w, r, err, fmt.Sprintf("Failed to encode J SON: %v", err))
571 return 571 return
572 } 572 }
573 } 573 }
574 574
575 func AddHandlers(r *mux.Router) { 575 func AddHandlers(r *mux.Router) {
576 r.HandleFunc("/"+ctfeutil.PAGE_SETS_PARAMETERS_POST_URI, pageSetsHandler ).Methods("POST") 576 r.HandleFunc("/"+ctfeutil.PAGE_SETS_PARAMETERS_POST_URI, pageSetsHandler ).Methods("POST")
577 } 577 }
OLDNEW
« no previous file with comments | « ct/go/ctfe/chromium_perf/chromium_perf.go ('k') | datahopper_internal/go/datahopper_internal/main.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698