Documentation
¶
Overview ¶
Package problems defines the application/problem+json (RFC 9457) types returned by the serve API and helpers for converting raw errors into them.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( InternalServerError = problem.Type{ URI: publicationServerBase + "internal", Status: http.StatusInternalServerError, Title: "Internal Server Error", } BadRequest = problem.Type{ URI: publicationServerBase + "bad-request", Status: http.StatusBadRequest, Title: "Bad Request", } NotFound = problem.Type{ URI: publicationServerBase + "not-found", Status: http.StatusNotFound, Title: "Not Found", } Forbidden = problem.Type{ URI: publicationServerBase + "forbidden", Status: http.StatusForbidden, Title: "Forbidden", } BadGateway = problem.Type{ URI: publicationServerBase + "bad-gateway", Status: http.StatusBadGateway, Title: "Bad Gateway", } NotImplemented = problem.Type{ URI: publicationServerBase + "not-implemented", Status: http.StatusNotImplemented, Title: "Not Implemented", } RangeNotSatisfiable = problem.Type{ URI: publicationServerBase + "range-not-satisfiable", Status: http.StatusRequestedRangeNotSatisfiable, Title: "Range Not Satisfiable", } URI: publicationServerBase + "service-unavailable", Status: http.StatusServiceUnavailable, Title: "Service Unavailable", } Gone = problem.Type{ URI: publicationServerBase + "gone", Status: http.StatusGone, Title: "Gone", } ReadingSessionRevoked = problem.Type{ URI: readingSessionBase + "revoked", Status: http.StatusForbidden, Title: "Reading session revoked", } ReadingSessionReturned = problem.Type{ URI: readingSessionBase + "returned", Status: http.StatusForbidden, Title: "Reading session returned", } ReadingSessionCancelled = problem.Type{ URI: readingSessionBase + "cancelled", Status: http.StatusForbidden, Title: "Reading session cancelled", } ReadingSessionExpired = problem.Type{ URI: readingSessionBase + "expired", Status: http.StatusGone, Title: "Reading session expired", } DeviceLimitExceeded = problem.Type{ URI: readingSessionBase + "device-limit-exceeded", Status: http.StatusTooManyRequests, Title: "Device limit exceeded", } )
Functions ¶
func From ¶
From maps a raw error to a *problem.Problem suitable for use as the fallback constructor passed to problem.WriteError. If the error tree already contains a *problem.Problem it is returned unchanged. Known sentinel errors are mapped to specific types; anything else becomes an InternalServerError with a generic detail (the original error is wrapped for logs only).
func FromResourceError ¶
func FromResourceError(rerr *fetcher.ResourceError) *problem.Problem
FromResourceError converts a fetcher.ResourceError to a Problem, picking the appropriate type from its HTTP status. The ResourceError is wrapped so its cause stays inspectable via errors.Is/As, but the client-facing detail is a fixed per-status string — the cause's text (which may include filesystem paths or upstream error specifics) is never serialized.
func Internal ¶
Internal returns an InternalServerError problem. The msg and (optional) wrapped err are preserved on the problem's internal error chain so they reach logs via Problem.Error(), but the client only ever sees the generic detail.
func IsClientDisconnect ¶ added in v0.9.0
IsClientDisconnect reports whether an error from serving a response means the client went away mid-request rather than anything failing server-side. Browsers routinely abort in-flight (range) requests while probing and seeking media, so these are expected noise and safe not to log.
ctx must be the request's own context. The server cancels it when the client goes away — for HTTP/2 aborts, before it fails the handler's Write (net/http's closeStream cancels the stream context before unblocking writes) — which is the only reliable way to recognize those: the errors themselves ("http2: stream closed", "client disconnected") are unexported sentinels out of errors.Is's reach. The errno checks cover HTTP/1 writes that fail before the server's background read notices the disconnect and cancels the context.
func Write ¶
func Write(err error, w http.ResponseWriter, r *http.Request)
Write writes err to w as an application/problem+json response. If err already contains a *problem.Problem in its tree it's used directly; otherwise From builds one. The library's internal logger is suppressed — callers should log at the call site (typically via slog) if they want a log line.
If writing the response body itself fails (e.g. client disconnected mid-write), the write error is logged here — by that point the status line and headers are already on the wire, so there is no way to recover. Client disconnect errors (see IsClientDisconnect) are ignored as expected noise.
Types ¶
This section is empty.