problems

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

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

View Source
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",
	}
	ServiceUnavailable = problem.Type{
		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

func From(err error) *problem.Problem

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

func Internal(msg string, err error) *problem.Problem

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 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 (EPIPE/ECONNRESET) are ignored as expected noise.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL