func handleRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) { // Some logic here to check if user is authorized if !isAuthorized(ctx) { errorMsg := fmt.Errorf("User is not authorized") http.Error(w, errorMsg.Error(), http.StatusUnauthorized) return } // code to handle request if user is authorized }
func handleRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) error { // Some logic here to check if the request has the appropriate headers if r.Header.Get("Auth-Token") == "" { return ctx.Errorf("Auth-Token header missing") } // code to handle request if headers are present return nil }In this example, if the necessary headers are missing, an error message is created using Context Errorf and returned as an error that can be handled by the calling function. Package Library: "context"