...

Source file src/github.com/sigstore/timestamp-authority/pkg/api/error.go

Documentation: github.com/sigstore/timestamp-authority/pkg/api

     1  // Copyright 2022 The Sigstore 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 api
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"regexp"
    21  
    22  	"github.com/go-openapi/runtime/middleware"
    23  	"github.com/mitchellh/mapstructure"
    24  
    25  	"github.com/sigstore/timestamp-authority/pkg/generated/models"
    26  	"github.com/sigstore/timestamp-authority/pkg/generated/restapi/operations/timestamp"
    27  	"github.com/sigstore/timestamp-authority/pkg/log"
    28  )
    29  
    30  const (
    31  	failedToGenerateTimestampResponse = "Error generating timestamp response"
    32  	WeakHashAlgorithmTimestampRequest = "Weak hash algorithm in timestamp request"
    33  )
    34  
    35  func errorMsg(message string, code int) *models.Error {
    36  	return &models.Error{
    37  		Code:    int64(code),
    38  		Message: message,
    39  	}
    40  }
    41  
    42  func handleTimestampAPIError(params interface{}, code int, err error, message string, fields ...interface{}) middleware.Responder {
    43  	if message == "" {
    44  		message = http.StatusText(code)
    45  	}
    46  
    47  	re := regexp.MustCompile("^(.*)Params$")
    48  	typeStr := fmt.Sprintf("%T", params)
    49  	handler := re.FindStringSubmatch(typeStr)[1]
    50  
    51  	logMsg := func(r *http.Request) {
    52  		log.RequestIDLogger(r).Errorw("exiting with error", append([]interface{}{"handler", handler, "statusCode", code, "clientMessage", message, "error", err}, fields...)...)
    53  		paramsFields := map[string]interface{}{}
    54  		if err := mapstructure.Decode(params, &paramsFields); err == nil {
    55  			log.RequestIDLogger(r).Debug(paramsFields)
    56  		}
    57  	}
    58  
    59  	switch params := params.(type) {
    60  	case timestamp.GetTimestampResponseParams:
    61  		logMsg(params.HTTPRequest)
    62  		switch code {
    63  		case http.StatusBadRequest:
    64  			return timestamp.NewGetTimestampResponseBadRequest().WithPayload(errorMsg(message, code))
    65  		case http.StatusNotImplemented:
    66  			return timestamp.NewGetTimestampResponseNotImplemented()
    67  		default:
    68  			return timestamp.NewGetTimestampResponseDefault(code).WithPayload(errorMsg(message, code))
    69  		}
    70  	case timestamp.GetTimestampCertChainParams:
    71  		logMsg(params.HTTPRequest)
    72  		switch code {
    73  		case http.StatusNotFound:
    74  			return timestamp.NewGetTimestampCertChainNotFound()
    75  		default:
    76  			return timestamp.NewGetTimestampCertChainDefault(code).WithPayload(errorMsg(message, code))
    77  		}
    78  	default:
    79  		log.Logger.Errorf("unable to find method for type %T; error: %v", params, err)
    80  		return middleware.Error(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
    81  	}
    82  }
    83  

View as plain text