Exemple #1
0
func TestErrors(t *testing.T) {
	assert.Equal(t, "Err1\nErr2\nErr3", util.Errors(
		errors.New("Err1"),
		errors.New("Err2"),
		errors.New("Err3"),
	))

	err := util.Errors(
		errgo.New("Err1"),
		errgo.New("Err2"),
		errors.New("Err3"),
	)
	assert.Contains(t, err, "corestoreio/csfw/util/errors_test.go:34\nErr2")
}
Exemple #2
0
// NewErrorFromErrors this lovely name describes that it can create
// a HTTP error from multiple error interfaces. The function util.Errors()
// will be used to extract the errgo.Locationer interface.
func NewErrorFromErrors(code int, errs ...error) *Error {
	e := &Error{Code: code, Message: http.StatusText(code)}
	if len(errs) > 0 {
		e.Message = util.Errors(errs...)
	}
	return e
}
Exemple #3
0
func (r *Router) recv(ctx context.Context, w http.ResponseWriter, req *http.Request) {
	if rcv := recover(); rcv != nil {
		if err := r.PanicHandler(WithContextPanic(ctx, rcv), w, req); err != nil {
			http.Error(w, util.Errors(err), http.StatusInternalServerError)
		}
	}
}
Exemple #4
0
func handleError(w http.ResponseWriter, errs ...error) {
	if len(errs) > 0 && errs[0] != nil {
		if ctxErr, ok := errs[0].(*ctxhttp.Error); ok {
			http.Error(w, ctxErr.Error(), ctxErr.Code)
			return
		}
	}
	http.Error(w, util.Errors(errs...), http.StatusInternalServerError)
}
Exemple #5
0
// WithAccessLog is a middleware that logs all access requests performed on the
// sub handler using github.com/corestoreio/csfw/net/ctxlog and
// github.com/rs/xstats stored in context.
func WithAccessLog() ctxhttp.Middleware {
	return func(h ctxhttp.HandlerFunc) ctxhttp.HandlerFunc {
		return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

			// Time request
			reqStart := time.Now()

			// Sniff the status and content size for logging
			lw := mutil.WrapWriter(w)

			err := h.ServeHTTPContext(ctx, lw, r)

			// Compute request duration
			reqDur := time.Since(reqStart)

			// Get request status
			status := ResponseStatus(ctx, lw.Status())

			// Log request stats
			sts := xstats.FromContext(ctx)
			tags := []string{
				"status:" + status,
				"status_code:" + strconv.Itoa(lw.Status()),
			}
			sts.Timing("request_time", reqDur, tags...)
			sts.Histogram("request_size", float64(lw.BytesWritten()), tags...)

			ctxlog.FromContext(ctx).Info("request",
				"error", util.Errors(err),
				"method", r.Method,
				"uri", r.URL.String(),
				"type", "access",
				"status", status,
				"status_code", lw.Status(),
				"duration", reqDur.Seconds(),
				"size", lw.BytesWritten(),
				"remote_addr", httputil.GetRemoteAddr(r).String(),
				"user_agent", r.Header.Get("User-Agent"),
				"referer", r.Header.Get("Referer"),
			)
			return err
		}
	}
}
Exemple #6
0
// Error implements the error interface. Returns a string where each error has
// been separated by a line break.
func (g *Group) Error() string {
	return util.Errors(g.lastErrors...)
}
Exemple #7
0
func (a arg) Error() string {
	return util.Errors(a.lastErrors...)
}
Exemple #8
0
// Error implements the error interface. Returns a string where each error has
// been separated by a line break.
func (dm *Daemon) Error() string {
	return util.Errors(dm.lastErrs...)
}
Exemple #9
0
// Error returns an error string
func (st *Storage) Error() string {
	return util.Errors(st.lastErrors...)
}
Exemple #10
0
// Error implements the error interface. Returns a string where each error has
// been separated by a line break.
func (w *Website) Error() string {
	return util.Errors(w.lastErrors...)
}
Exemple #11
0
// Error returns an error string
func (s *Service) Error() string {
	return util.Errors(s.lastErrors...)
}