mirror of
https://github.com/Fishwaldo/huma.git
synced 2025-03-16 03:41:42 +00:00
fix: various PR feedback
This commit is contained in:
parent
f1f9c85071
commit
5a6a1c93d2
5 changed files with 38 additions and 5 deletions
10
context.go
10
context.go
|
@ -85,7 +85,7 @@ func (c *hcontext) WriteHeader(status int) {
|
|||
}
|
||||
|
||||
if !found {
|
||||
panic(fmt.Errorf("Header %s is not declared for %s %s (allowed: %s)", name, c.r.Method, c.r.URL.Path, allowed))
|
||||
panic(fmt.Errorf("Response header %s is not declared for %s %s with status code %d (allowed: %s)", name, c.r.Method, c.r.URL.Path, status, allowed))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -102,6 +102,10 @@ func (c *hcontext) Write(data []byte) (int, error) {
|
|||
}
|
||||
|
||||
func (c *hcontext) WriteError(status int, message string, errors ...error) {
|
||||
if c.closed {
|
||||
panic(fmt.Errorf("Trying to write to response after WriteModel or WriteError for %s %s", c.r.Method, c.r.URL.Path))
|
||||
}
|
||||
|
||||
details := []*ErrorDetail{}
|
||||
|
||||
c.errors = append(c.errors, errors...)
|
||||
|
@ -135,6 +139,10 @@ func (c *hcontext) WriteError(status int, message string, errors ...error) {
|
|||
}
|
||||
|
||||
func (c *hcontext) WriteModel(status int, model interface{}) {
|
||||
if c.closed {
|
||||
panic(fmt.Errorf("Trying to write to response after WriteModel or WriteError for %s %s", c.r.Method, c.r.URL.Path))
|
||||
}
|
||||
|
||||
// Get the negotiated content type the client wants and we are willing to
|
||||
// provide.
|
||||
ct := selectContentType(c.r)
|
||||
|
|
6
error.go
6
error.go
|
@ -9,9 +9,9 @@ type ErrorDetailer interface {
|
|||
|
||||
// ErrorDetail provides details about a specific error.
|
||||
type ErrorDetail struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Value interface{} `json:"value,omitempty"`
|
||||
Message string `json:"message,omitempty" doc:"Error message text"`
|
||||
Location string `json:"location,omitempty" doc:"Where the error occured, e.g. 'body.items[3].tags' or 'path.thing-id'"`
|
||||
Value interface{} `json:"value,omitempty" doc:"The value at the given location"`
|
||||
}
|
||||
|
||||
// Error returns the error message / satisfies the `error` interface.
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
@ -22,6 +23,11 @@ var logConfig zap.Config
|
|||
// middleware. This can be changed dynamically at runtime.
|
||||
var LogLevel *zap.AtomicLevel
|
||||
|
||||
// LogTracePrefix is used to prefix OpenTracing trace and span ID key names in
|
||||
// emitted log message tag names. Use this to integrate with DataDog and other
|
||||
// tracing service providers.
|
||||
var LogTracePrefix = "dd."
|
||||
|
||||
// NewDefaultLogger returns a new low-level `*zap.Logger` instance. If the
|
||||
// current terminal is a TTY, it will try ot use colored output automatically.
|
||||
func NewDefaultLogger() (*zap.Logger, error) {
|
||||
|
@ -86,6 +92,17 @@ func Logger(next http.Handler) http.Handler {
|
|||
zap.String("http.url", r.URL.String()),
|
||||
zap.String("network.client.ip", r.RemoteAddr),
|
||||
)
|
||||
|
||||
if span := opentracing.SpanFromContext(r.Context()); span != nil {
|
||||
// We have a span context, so log its info to help with correlation.
|
||||
if sc, ok := span.Context().(spanContext); ok {
|
||||
contextLog = contextLog.With(
|
||||
zap.Uint64(LogTracePrefix+"trace_id", sc.TraceID()),
|
||||
zap.Uint64(LogTracePrefix+"span_id", sc.SpanID()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
r = r.WithContext(context.WithValue(r.Context(), logContextKey, contextLog.Sugar()))
|
||||
nw := &statusRecorder{ResponseWriter: w}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ func (w *minimalWriter) Write(data []byte) (int, error) {
|
|||
}
|
||||
|
||||
func (w *minimalWriter) WriteHeader(statusCode int) {
|
||||
if statusCode >= 200 && statusCode < 300 {
|
||||
if statusCode >= 200 && statusCode < 300 && statusCode != 201 {
|
||||
statusCode = http.StatusNoContent
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,14 @@ import (
|
|||
"github.com/opentracing/opentracing-go/ext"
|
||||
)
|
||||
|
||||
type spanContext interface {
|
||||
// SpanID returns the span ID that this context is carrying.
|
||||
SpanID() uint64
|
||||
|
||||
// TraceID returns the trace ID that this context is carrying.
|
||||
TraceID() uint64
|
||||
}
|
||||
|
||||
// OpenTracing provides a middleware for cross-service tracing support.
|
||||
func OpenTracing(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
Loading…
Add table
Reference in a new issue