github.com/m3db/m3@v1.5.0/src/ctl/service/r2/handler.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package r2
    22  
    23  import (
    24  	"fmt"
    25  	"net/http"
    26  
    27  	"github.com/m3db/m3/src/ctl/auth"
    28  	xhttp "github.com/m3db/m3/src/x/net/http"
    29  
    30  	"go.uber.org/zap"
    31  )
    32  
    33  type r2HandlerFunc func(http.ResponseWriter, *http.Request) error
    34  
    35  type r2Handler struct {
    36  	logger *zap.Logger
    37  	auth   auth.HTTPAuthService
    38  }
    39  
    40  func (h r2Handler) wrap(authType auth.AuthorizationType, fn r2HandlerFunc) http.Handler {
    41  	f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    42  		if err := fn(w, r); err != nil {
    43  			h.handleError(w, err)
    44  		}
    45  	})
    46  	return h.auth.NewAuthHandler(authType, f, writeAPIResponse)
    47  }
    48  
    49  func (h r2Handler) handleError(w http.ResponseWriter, opError error) {
    50  	h.logger.Error(opError.Error())
    51  
    52  	var err error
    53  	switch opError.(type) {
    54  	case conflictError:
    55  		err = writeAPIResponse(w, http.StatusConflict, opError.Error())
    56  	case badInputError:
    57  		err = writeAPIResponse(w, http.StatusBadRequest, opError.Error())
    58  	case versionError:
    59  		err = writeAPIResponse(w, http.StatusConflict, opError.Error())
    60  	case notFoundError:
    61  		err = writeAPIResponse(w, http.StatusNotFound, opError.Error())
    62  	case authError:
    63  		err = writeAPIResponse(w, http.StatusUnauthorized, opError.Error())
    64  	default:
    65  		err = writeAPIResponse(w, http.StatusInternalServerError, opError.Error())
    66  	}
    67  
    68  	// Getting here means that the error handling failed. Trying to convey what was supposed to happen.
    69  	if err != nil {
    70  		msg := fmt.Sprintf("Could not generate error response for: %s", opError.Error())
    71  		h.logger.Error(msg)
    72  		xhttp.WriteError(w, err)
    73  	}
    74  }