...

Source file src/github.com/palantir/go-baseapp/baseapp/error.go

Documentation: github.com/palantir/go-baseapp/baseapp

     1  // Copyright 2018 Palantir Technologies, Inc.
     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 baseapp
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	"github.com/bluekeyes/hatpear"
    23  	"github.com/palantir/go-baseapp/pkg/errfmt"
    24  	"github.com/pkg/errors"
    25  	"github.com/rs/zerolog"
    26  	"github.com/rs/zerolog/hlog"
    27  )
    28  
    29  // httpError represents any error that presents itself as an HTTP error with a
    30  // status code.
    31  type httpError interface {
    32  	StatusCode() int
    33  }
    34  
    35  // RichErrorMarshalFunc is a zerolog error marshaller that formats the error as
    36  // a string that includes a stack trace, if one is available.
    37  func RichErrorMarshalFunc(err error) interface{} {
    38  	switch err := err.(type) {
    39  	case hatpear.PanicError:
    40  		return fmt.Sprintf("%+v", err)
    41  	default:
    42  		return errfmt.Print(err)
    43  	}
    44  }
    45  
    46  // HandleRouteError is a hatpear error handler that logs the error and sends
    47  // an error response to the client. If the error has a `StatusCode` function
    48  // this will be called and converted to an appropriate HTTP status code error.
    49  func HandleRouteError(w http.ResponseWriter, r *http.Request, err error) {
    50  	var log *zerolog.Event
    51  	// Either the deadline has passed or the request was canceled
    52  	// 499 is an NGINX style response code for 'Client Closed Connection'
    53  	// and is a non-standard, but widely used, HTTP status code
    54  	if cerr := r.Context().Err(); cerr == context.Canceled {
    55  		log = hlog.FromRequest(r).Debug()
    56  		WriteJSON(w, 499, map[string]string{
    57  			"error": "Client Closed Connection",
    58  		})
    59  	} else {
    60  		log = hlog.FromRequest(r).Error().Err(err)
    61  
    62  		cause := errors.Cause(err)
    63  		statusCode := http.StatusInternalServerError
    64  		if aerr, ok := cause.(httpError); ok {
    65  			statusCode = aerr.StatusCode()
    66  		}
    67  
    68  		rid, _ := hlog.IDFromRequest(r)
    69  		WriteJSON(w, statusCode, map[string]string{
    70  			"error":      http.StatusText(statusCode),
    71  			"request_id": rid.String(),
    72  		})
    73  	}
    74  
    75  	log.Str("method", r.Method).
    76  		Str("path", r.URL.String()).
    77  		Msg("Unhandled error while serving route")
    78  }
    79  

View as plain text