Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: write errors/warnings to stderr rather than stdout #651

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
sm3142 marked this conversation as resolved.
Show resolved Hide resolved
}
}()
newType := reflect.StructOf(fields)
Expand Down
Loading