gopkg.in/simversity/gottp.v3@v3.0.0-20160401065405-576cf030ca0e/exception.go (about)

     1  package gottp
     2  
     3  import (
     4  	"bytes"
     5  	"html/template"
     6  
     7  	utils "gopkg.in/simversity/gottp.v3/utils"
     8  )
     9  
    10  const errorExtra = `
    11  Host: {{.Host}}
    12  Method: {{.Method}}
    13  Protocol: {{.Protocol}}
    14  RemoteIP: {{.RemoteIP}}
    15  URI: {{.URI}}
    16  Referer: {{.Referer}}
    17  Arguments: {{.Arguments}}
    18  `
    19  
    20  type ErrorStack struct {
    21  	Host      string
    22  	Method    string
    23  	Protocol  string
    24  	RemoteIP  string
    25  	URI       string
    26  	Referer   string
    27  	Arguments string
    28  }
    29  
    30  func getTracerExtra(req *Request) func(reason string) string {
    31  	return func(reason string) string {
    32  		e := HttpError{500, "Internal Server Error: " + reason}
    33  		req.Raise(e)
    34  
    35  		if req == nil {
    36  			return ""
    37  		}
    38  
    39  		var referrer string
    40  		if len(req.Request.Header["Referer"]) > 0 {
    41  			referrer = req.Request.Header["Referer"][0]
    42  		}
    43  
    44  		params := string(utils.Encoder(req.GetArguments()))
    45  
    46  		stack := ErrorStack{
    47  			Host:      req.Request.Host,
    48  			Method:    req.Request.Method,
    49  			Protocol:  req.Request.Proto,
    50  			RemoteIP:  req.Request.RemoteAddr,
    51  			URI:       req.Request.URL.Path,
    52  			Referer:   referrer,
    53  			Arguments: params,
    54  		}
    55  
    56  		var doc bytes.Buffer
    57  
    58  		t := template.Must(template.New("error_email").Parse(errorExtra))
    59  		t.Execute(&doc, stack)
    60  
    61  		return doc.String()
    62  	}
    63  }