Skip to content

Commit

Permalink
ociregistry/ociserver: pass request into WriteError
Browse files Browse the repository at this point in the history
This gives the `WriteError` function the capability to
inspect the request in order to form the error (for example
to extract the context or log some request parameters).
We also change its signature so it's clear that the
error is discarded - in general this is the strategy taken
by most HTTP servers anyway as clients tend to
disappear without warning, so logging errors in this case is just noise.
We leave the signature of `ociregistry.WriteError` the same
so it's possible for a caller to use that error if they want,
and also to make it clear that it does not use the `http.Request`
to inform the result.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ifdbc2ea509385b28479a064f4c2b13686cba5f7f
Dispatch-Trailer: {"type":"trybot","CL":1193024,"patchset":1,"ref":"refs/changes/24/1193024/1","targetBranch":"main"}
  • Loading branch information
rogpeppe authored and porcuepine committed Apr 12, 2024
1 parent 4dc200d commit ea8f8e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ociregistry/ociserver/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestCustomErrorWriter(t *testing.T) {
// HTTP status code is derived from the OCI error code in preference
// to the HTTPError status code.
r := New(&ociregistry.Funcs{}, &Options{
WriteError: func(w http.ResponseWriter, err error) error {
WriteError: func(w http.ResponseWriter, _ *http.Request, err error) {
w.Header().Set("Some-Header", "a value")
return ociregistry.WriteError(w, err)
ociregistry.WriteError(w, err)
},
})
s := httptest.NewServer(r)
Expand Down
16 changes: 10 additions & 6 deletions ociregistry/ociserver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ var v2 = ocispecroot.Versioned{
// Options holds options for the server.
type Options struct {
// WriteError is used to write error responses. It is passed the
// error an API call has returned and is responsible for writing
// it to w. If WriteError is nil, [ociregistry.WriteError] will
// be used.
WriteError func(w http.ResponseWriter, err error) error
// writer to write the error response to, the request that
// the error is in response to, and the error itself.
//
// If WriteError is nil, [ociregistry.WriteError] will
// be used and any error discarded.
WriteError func(w http.ResponseWriter, req *http.Request, err error)

// DisableReferrersAPI, when true, causes the registry to behave as if
// it does not understand the referrers API.
Expand Down Expand Up @@ -140,7 +142,9 @@ func New(backend ociregistry.Interface, opts *Options) http.Handler {
r.opts.DebugID = fmt.Sprintf("ociserver%d", atomic.AddInt32(&debugID, 1))
}
if r.opts.WriteError == nil {
r.opts.WriteError = ociregistry.WriteError
r.opts.WriteError = func(w http.ResponseWriter, _ *http.Request, err error) {
ociregistry.WriteError(w, err)
}
}
return r
}
Expand Down Expand Up @@ -176,7 +180,7 @@ var handlers = []func(r *registry, ctx context.Context, w http.ResponseWriter, r

func (r *registry) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if rerr := r.v2(resp, req); rerr != nil {
r.opts.WriteError(resp, rerr)
r.opts.WriteError(resp, req, rerr)
return
}
}
Expand Down

0 comments on commit ea8f8e9

Please sign in to comment.