go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/ui/errors.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ui
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"go.chromium.org/luci/server/router"
    21  	"go.chromium.org/luci/server/templates"
    22  )
    23  
    24  // List of all possible error messages presented to the user.
    25  var (
    26  	uiErrNoJobOrNoPerm = presentableError{
    27  		Status:         http.StatusNotFound,
    28  		Message:        "No such job or no permission to view it.",
    29  		ReloginMayHelp: true,
    30  	}
    31  
    32  	uiErrBadInvocationID = presentableError{
    33  		Status:         http.StatusBadRequest,
    34  		Message:        "Malformed invocation ID.",
    35  		ReloginMayHelp: false,
    36  	}
    37  
    38  	uiErrNoInvocation = presentableError{
    39  		Status:         http.StatusNotFound,
    40  		Message:        "The requested invocation is not found.",
    41  		ReloginMayHelp: false,
    42  	}
    43  
    44  	uiErrActionForbidden = presentableError{
    45  		Status:         http.StatusForbidden,
    46  		Message:        "No permission to execute the attempted action.",
    47  		ReloginMayHelp: true,
    48  	}
    49  
    50  	uiErrCannotTriggerPausedJob = presentableError{
    51  		Status:         http.StatusConflict,
    52  		Message:        "Cannot trigger a paused job. Resume it first.",
    53  		ReloginMayHelp: false,
    54  	}
    55  )
    56  
    57  // presentableError defines an error message we render as a pretty HTML page.
    58  //
    59  // All possible kinds of errors are instantiated above.
    60  type presentableError struct {
    61  	Status         int    // HTTP status code
    62  	Message        string // the main message on the page
    63  	ReloginMayHelp bool   // if true, display a suggestion to login/relogin
    64  }
    65  
    66  // render renders the HTML into the writer and sets the response code.
    67  func (e *presentableError) render(c *router.Context) {
    68  	breadcrumps := []string{}
    69  	for _, k := range []string{"ProjectID", "JobName", "InvID"} {
    70  		p := c.Params.ByName(k)
    71  		if p == "" {
    72  			break
    73  		}
    74  		breadcrumps = append(breadcrumps, p)
    75  	}
    76  	c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
    77  	c.Writer.WriteHeader(e.Status)
    78  	templates.MustRender(c.Request.Context(), c.Writer, "pages/error.html", map[string]any{
    79  		"Breadcrumps":    breadcrumps,
    80  		"LastCrumbIdx":   len(breadcrumps) - 1,
    81  		"Message":        e.Message,
    82  		"ReloginMayHelp": e.ReloginMayHelp,
    83  	})
    84  }