Expose version/build metadata and improve provider error messaging

This commit is contained in:
2025-10-01 22:21:44 +02:00
parent a3e6b105d0
commit 7d4d56671f
5 changed files with 278 additions and 27 deletions

View File

@@ -1,5 +1,10 @@
package openai
import (
"fmt"
"strings"
)
// ChatMessage represents a single message within a chat completion request or response.
type ChatMessage struct {
Role string `json:"role"`
@@ -50,6 +55,48 @@ type ChatCompletionStreamEvent struct {
// ChatCompletionStreamHandler consumes streaming completion events.
type ChatCompletionStreamHandler func(ChatCompletionStreamEvent) error
// RequestError captures an error response returned by the API together with the HTTP status code.
type RequestError struct {
Status int
Response ErrorResponse
}
// Error implements the error interface.
func (e *RequestError) Error() string {
if e == nil {
return ""
}
msg := strings.TrimSpace(e.Response.Error.Message)
if msg == "" {
return fmt.Sprintf("api error (status %d)", e.Status)
}
return fmt.Sprintf("api error (status %d): %s", e.Status, msg)
}
// StatusCode returns the originating HTTP status code.
func (e *RequestError) StatusCode() int {
if e == nil {
return 0
}
return e.Status
}
// Message returns the raw message provided by the API, if any.
func (e *RequestError) Message() string {
if e == nil {
return ""
}
return e.Response.Error.Message
}
// Type returns the OpenAI error type string, when present.
func (e *RequestError) Type() string {
if e == nil {
return ""
}
return e.Response.Error.Type
}
// APIError captures structured error responses returned by the API.
type APIError struct {
Message string `json:"message"`