Skip to content

Commit

Permalink
Merge pull request #651 from sm3142/write-errors-to-stderr
Browse files Browse the repository at this point in the history
fix: write errors/warnings to stderr rather than stdout
  • Loading branch information
danielgtaylor authored Nov 14, 2024
2 parents f71eb50 + 7e4e027 commit bb5d167
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"os"
)

// ErrorDetailer returns error details for responses & debugging. This enables
Expand Down Expand Up @@ -264,7 +265,7 @@ func WriteErr(api API, ctx Context, status int, msg string, errs ...error) error
writeErr := writeResponse(api, ctx, status, "", err)
if writeErr != nil {
// If we can't write the error, log it so we know what happened.
fmt.Printf("could not write error: %s\n", writeErr)
fmt.Fprintf(os.Stderr, "could not write error: %v\n", writeErr)
}
return writeErr
}
Expand Down
4 changes: 2 additions & 2 deletions humacli/humacli.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *cli[O]) setupOptions(t reflect.Type, path []int) {
if !field.IsExported() {
// This isn't a public field, so we cannot use reflect.Value.Set with
// it. This is usually a struct field with a lowercase name.
fmt.Println("warning: ignoring unexported options field", field.Name)
fmt.Fprintln(os.Stderr, "warning: ignoring unexported options field", field.Name)
continue
}

Expand Down Expand Up @@ -298,7 +298,7 @@ func New[O any](onParsed func(Hooks, *O)) CLI {
// Server is done, just exit.
case <-quit:
if c.stop != nil {
fmt.Println("Gracefully shutting down the server...")
fmt.Fprintln(os.Stderr, "Gracefully shutting down the server...")
c.stop()
}
}
Expand Down
9 changes: 5 additions & 4 deletions sse/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"reflect"
"runtime/debug"
"time"
Expand Down Expand Up @@ -160,10 +161,10 @@ func Register[I any](api huma.API, op huma.Operation, eventTypeMap map[string]an
send := func(msg Message) error {
if deadliner != nil {
if err := deadliner.SetWriteDeadline(time.Now().Add(WriteTimeout)); err != nil {
fmt.Println("warning: unable to set write deadline: " + err.Error())
fmt.Fprintf(os.Stderr, "warning: unable to set write deadline: %v\n", err)
}
} else {
fmt.Println("warning: unable to set write deadline")
fmt.Fprintln(os.Stderr, "write deadline not supported by underlying writer")
}

// Write optional fields
Expand All @@ -176,7 +177,7 @@ func Register[I any](api huma.API, op huma.Operation, eventTypeMap map[string]an

event, ok := typeToEvent[deref(reflect.TypeOf(msg.Data))]
if !ok {
fmt.Println("error: unknown event type", reflect.TypeOf(msg.Data))
fmt.Fprintf(os.Stderr, "error: unknown event type %v\n", reflect.TypeOf(msg.Data))
debug.PrintStack()
}
if event != "" && event != "message" {
Expand All @@ -198,7 +199,7 @@ func Register[I any](api huma.API, op huma.Operation, eventTypeMap map[string]an
if flusher != nil {
flusher.Flush()
} else {
fmt.Println("error: unable to flush")
fmt.Fprintln(os.Stderr, "error: unable to flush")
return fmt.Errorf("unable to flush: %w", http.ErrNotSupported)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package huma
import (
"bytes"
"fmt"
"os"
"path"
"reflect"
)
Expand Down Expand Up @@ -132,7 +133,7 @@ func (t *SchemaLinkTransformer) OnAddOperation(oapi *OpenAPI, op *Operation) {
// Catch some scenarios that just aren't supported in Go at the
// moment. Logs an error so people know what's going on.
// https://github.com/danielgtaylor/huma/issues/371
fmt.Println("Warning: unable to create schema link for type", typ, ":", r)
fmt.Fprintln(os.Stderr, "Warning: unable to create schema link for type", typ, ":", r)
}
}()
newType := reflect.StructOf(fields)
Expand Down

0 comments on commit bb5d167

Please sign in to comment.